jdbc简介: JDBC全称: java database connectivity,简称jdbc,翻译就是Java数据库连接。
jdbc的作用: 是一个标准的java应用编程接口(java API),用来连接Java编程语言和广泛的数据库。通过jdbc代码实现对数据库的操作。 jdbc执行步骤: 导入jar包步骤: 1,在web-INF目录下创建一个lib包,专门放jar包 2,将jar包复制到lib路径下 3,右键单击,选择 add as library 1,注册驱动:
Class.forName("");
2,获取连接:
Connection connection = DriverManager.getConnection(url, username, password);
3,定义SQL语句:
String sql = "insert into stus values(?,?)";
4,获取执行SQL对象:
PreparedStatement ps = con.prepareStatement(sql);
5,执行SQL语句:
ps.executeUpdate();
6,处理结果:
ResultSet rs = ps.executeQuery();
while(rs.next){
}
7,释放资源:
rs.close();
ps.close();
con.close();
DriverManager类: DriverManager类下面提供的全是静态方法,只需要用类名点方法就可以调用,不需要实例化。
主要作用: 1,注册驱动:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
2,获取连接:
Connection connection = DriverManager.getConnection(url, username, password);
Connection类: 主要作用: 1,获取执行sql的对象:
Statement statement = connection.createStatement();
2,管理事务: mysql中事务管理通过begin开启事务,rollback回滚事务,commit提交事务。在jdbc里面通过connection类的特定方法也可以实现这三个功能。 管理事务的用法:
//注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//获取连接
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=meitao","sa","123");
//定义SQL语句
String sql = "insert into stus values(?,?)";
//获取执行SQL对象
PreparedStatement ps = con.prepareStatement(sql);
try {
//开启事务
con.setAutoCommit(false);
ps.setString(1,"哈哈哈");
ps.setString(2,"666");
int row = ps.executeUpdate();
if (row != 0){
//提交事务
con.commit();
System.out.println("添加成功");
}
}catch (Exception e){
//回滚事务
con.rollback();
e.printStackTrace();
}
ps.close();
con.close();
ResultSet类: 只有执行查询的时候,才有这个ReesultSet
ResultSet resultSet = statement.executeQuery(sql);
next() 方法:
返回Boolean类型,判断当前行是否有有效数据,若有就返回true,否则返回false。 getXXX()方法:
对应数据类型定义对应方法。传参可以为字段名,也可以是列对应的编号。 示例:
while (rs.next()){
int id = rs.getInt("id");
String adminName = rs.getString("adminName");
String adminPwd = rs.getString("adminPwd");
System.out.println("编号:"+id+" 姓名:"+adminName+"密 码:"+adminPwd);
}
Statement和Prepared Statement和CallableStatement: Statement和PreparedStatement和CallableStatement 都是接口。
Statement继承 <-PreparedStatement继承 <- CallableStatement
编译执行编译执行 预编译在执行 可以调用存储过程
Statement: 用于执行见到那的SQL语句,不带参数。Statement 接口不接受参数。 //定义SQL语句
int id = 1;
String pwd = "123";
String sql = "select * from stus where id = '"+id+"' and adminPwd = '"+pwd+"' ";
//获取执行SQL对象
Statement state = con.createStatement();
ResultSet rs = state.executeQuery(sql);
PreparedStatement: 用于执行预编译的 SQL 语句,可以带输入参数。 示例:
//定义SQL语句
String sql = "insert into stus values(?,?)";
//获取执行SQL对象
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,"哈哈哈");
ps.setString(2,"666");
CallableStatement : 用于执行数据库存储过程,可以带输入和输出参数。 现在数据库中创建存储过程:
create proc proc_selectAll
as select * from stus
go
在java中测试:
String sql = "exec proc_selectAll"; //调用存储过程
CallableStatement call = con.prepareCall(sql);
ResultSet rs = call.executeQuery();
while (rs.next()){
int id = rs.getInt("id");
String adminName = rs.getString("adminName");
String adminPwd = rs.getString("adminPwd");
System.out.println("编号:"+id+" 姓名:"+adminName+"密 码:"+adminPwd);
}
在数据库中定义一个删除的存储过程:
//定义SQL语句:
String sql = "{call proc_DelById(?)}";
CallableStatement callt = con.prepareCall(sql);
callt.setInt(1,2);
int row = callt.executeUpdate();
在数据中创建修改的存储过程:
create proc proc_updae(@Sname varchar(20),@Spwd varchar(20),@Sid int)
as update stus set adminName = @Sname,adminPwd = @Spwd where id = @Sid
go
//定义SQL语句:
String sql = "{call proc_updae(?,?,?)}";
CallableStatement call = con.prepareCall(sql);
call.setString(1,"里哈哈哈");
call.setString(2,"20");
call.setInt(3,1);
int rows = call.executeUpdate();