JDBC中对数据库做增删改操作
对数据库中的操作只有俩种方式,增删改的操作是一种,查询时一种,增删改没有结果集,而查询有结果集,接下来我就先介绍增删改的通用操作代码
public void testInsert(){
Connection connection = null;
PreparedStatement ps = null;
try {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
//4,预编译sql语句,返回PreparedStatement实例
String sql = "insert into users(id,name,password,email,birthday)values(?,?,?,?,?)";
ps = connection.prepareStatement(sql);
//5,填充占位符
ps.setInt(1,5);
ps.setString(2,"罗志祥");
ps.setString(3,"123147");
ps.setString(4,"www@qq.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("1000-01-01");
ps.setDate(5,new java.sql.Date(date.getTime()));
//6,执行操作
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//7,资源的关闭
try {
if (ps != null)
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (connection != null)
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
第一步就是先读入jdbc.properties文件中的四个基本信息
第二步是加载驱动
第三步是获取连接
第四步预编译sql语句,返回preparedStatement实例
第五步填充占位符:上边的代码只是第一遍做,熟悉了一下过程,如果在我们不知道类型是,一般会选择setObjec()方法
第六步是执行操作了:除了execute()方法,我们一般也会写成executeUpdate()方法,execute()返回的是boolean类型的值,这个方法指的是如果返回true,则表示有结果集,如果返回false,就表示没有结果集,而executeUpdate()方法,则返回的是一个int类型的值,指的是被影响到的行数,比如说删除多少行,或者修改多少行这样的
第七步是资源的关闭。
通过反复的学习,发现第一步第二步,第三步和第七步都是固定的套路,所以可以写到一个工具类里
例如
public class JDBCUtils {
public static Connection getConnection() throws Exception {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
public static void closeResource(Connection connection, Statement ps){
try {
if (ps != null)
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (connection != null)
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
这样下次我们就可以直接调用已经写好的方法,不用再重复写固定的代码了
然后接下来就是写一个通用的增删改操作,来,直接上代码
public void update(String sql,Object ...args){
Connection connection = null;
PreparedStatement ps = null;
try {
//1,获取数据库的连接
connection = JDBCUtils.getConnection();
//2,预编译sql语句
ps = connection.prepareStatement(sql);
//3,填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1,args[i]);
}
//4,执行操作
ps.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
//5,关闭资源
JDBCUtils.closeResource(connection,ps);
}
}
这次就只有这五步了:
第一步:这是学习jdbc的必须的一个步骤,只是这里调用了已经写好的方法
第二步:预编译sql语句
第三步:填充占位符:这一步尤为关键:i+1表示的是sql中的位置,args[i]:可变参数占位符,有几个?就写几个值进来
第四步:执行操作
第五步:关闭资源也是直接调用已经写好的方法
最后写一个测试的代码
public void testUpdate1(){
String sql = "delete from users where id = ?";
update(sql,2);
String sql = "update account set name = ? where id = ?";
update(sql,"DD","2");
这里写了一个删除的测试,一个更新的测试,可以对照上便代码进行学习
好了,这就是增删改的全部内容,我的下一步博客会更新查询操作,希望大家持续关注,感谢
浙公网安备 33010602011771号