CRUD_PreparedStatement
package songyan.jdbc.crud; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import songyan.jdbc.util.DBUtil;
import songyan.jdbc.entity.*; public class CRUD_prepared{ public static void selectTest() throws Exception
{
Connection conn=null;
PreparedStatement sta=null;
ResultSet rs=null;
String sql="select * from users where name=? and password=?"; conn=DBUtil.getConnection(); sta=conn.prepareStatement(sql);
sta.setString(1, "zhansan");
sta.setString(2, "123"); rs=sta.executeQuery(); List<User> l= new ArrayList<User>();
while(rs.next())
{
User u= new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
u.setEmail(rs.getString("email"));
u.setBirthday(rs.getDate("birthday"));
l.add(u);
} for(User u:l)
{
System.out.println(u.getId()+" "+u.getName());
} DBUtil.closeAll(conn, sta, rs); } public static void insertTest() throws SQLException
{
Connection conn=null;
PreparedStatement sta=null;
ResultSet rs=null;
String sql="insert into users values(1,'a0','b0','a@163.com','1981-12-04')"; conn=DBUtil.getConnection(); sta=conn.prepareStatement(sql); System.out.println(sta.executeUpdate()); DBUtil.closeAll(conn, sta, rs);
} public static void updateTest() throws SQLException
{
Connection conn=null;
PreparedStatement sta=null;
ResultSet rs=null;
String sql="update users set name='lisi' where id='4'";
conn=DBUtil.getConnection(); sta=conn.prepareStatement(sql); System.out.println("影响了"+sta.executeUpdate()); DBUtil.closeAll(conn, sta, rs);
} public static void deleteTest() throws SQLException
{
Connection conn=null;
PreparedStatement sta=null;
ResultSet rs=null;
String sql="delete from users where name='bbb'"; conn=DBUtil.getConnection(); sta=conn.prepareStatement(sql);
System.out.println(sta.executeUpdate()); DBUtil.closeAll(conn, sta, rs);
} public static void main(String[] args) throws Exception
{
deleteTest();
}
}
CRUD_PreparedStatement的更多相关文章
随机推荐
- log4j配置打印mybatis的sql到控制台(复制)
log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender ...
- 清除浮动float (:after方法)
1. 什么时候需要清除浮动?清除浮动有哪些方法? (1)对元素进行了浮动(float)后,该元素就会脱离文档流,浮动在文档之上.在CSS中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是 ...
- ubuntu16.04 搭建nexus+maven 学习
/opt/nexus-2.10.0-02/bin vim nexus 关键配置: RUN_AS_USER=root JAVA_HOME=/usr/lib/java/jre export NEXUS_H ...
- 如何从List中删除元素
从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo { public static void main(Str ...
- [CF1045C]Hyperspace Highways
题目大意:给一张$n$个点$m$条边的图,保证若有一个环,一定是完全子图,多次询问两个点之间的最短路径长度 题解:把完全子图缩成一个点,圆方树,方点权值设成$1$,圆点设成$0$即可. 卡点:数组开小 ...
- [洛谷P4588][TJOI2018]数学计算
题目大意:有一个数$x$和取模的数$mod$,初始为$1$,有两个操作: $m:x=x\times m$并输出$x\% mod$ $pos:x=x/第pos次操作乘的数$(保证合法),并输出$x\%m ...
- 进程管理利器Supervisor--入门简介
目录 概述 Supervisor是什么 Supervisor意图 Supervisor特性 Supervisor组件 平台需求 概述 项目运行需要后台运行,一般都是使用 nohup,但是nohup不能 ...
- ACM-Hero In Maze
Hero In Maze 时间限制(普通/Java):1000MS/10000MS 运行 ...
- Codeforces Round #328 (Div. 2) B
B. The Monster and the Squirrel time limit per test 1 second memory limit per test 256 megabytes inp ...
- 配置postfix支持虚拟域和虚拟用户
请先看基础篇 https://www.cnblogs.com/hellojackyleon/p/9281620.html https://sourceforge.net/projects/couri ...