使用Apache下的DBUtil
使用Apache下的DBUtil,
jar包直接从网上下就可以了
导入这个包

1.建立连接
Connection connection = JDBCUtil.getConnection();
这里的JDBCUtil是个工具类,返回的是数据库连接
2.sql语句
String sql ="select * from student";
String sql1 ="delete from student where stuname =?";
3.创建QueryRunner对象,这个对象中为我们封装好了一系列数据操作,便于我们操作数据库
QueryRunner runner = new QueryRunner();
4.发送请求
List<StudentEntity> query = runner.query(connection, sql, new BeanListHandler<>(StudentEntity.class));
其中增修改删除都是可以通过下面的这个方法 update,在sql语句后可以放多个 不定对象,用于替换sql中的?
int update = runner.update(connection, sql1,"lisi");
System.out.println("受影响的行数:"+update);
5.断开链接
connection.close();
本人是个刚入职的小菜鸡,写下来只是一些随笔,用来自己回顾,很多东西不一定正确,只是我当下自己的理解,请各位大神,有错误的地方可以指出来哈。

浙公网安备 33010602011771号