JDBC
1. 使用jdbc步骤
a.导入数据库厂商提供的驱动程序(导入jar包)
b.加载驱动程序
Class.forName("驱动程序")
c.获得连接
Connection conn = DriverManager.getConnection(url,username,password);
url = “jdbc:数据库://ip地址:端口;数据库”;
username:“用户名”;
password:“密码”
d.获得执行sql对象,执行sql返回结果
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql语句);
e.处理结果集
while(rs.next){
Xxxx s =rs.getXxxx(“字段名|数字”)
}
注意:数字下标从1开始
f.释放资源
rs.close();
stat.close();
conn.close();
package day28; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Collection; public class TestJDBC { public static void main(String[] args) { // TODO Auto-generated method stub //1.加载驱动 try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //2.获得连接 url =jdbc: sqlserver://localhost:1433;DatabaseName = schoolDB //username,passwprd //jdbc:数据库://IP地址:端口号数据库名 try { Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;DatabaseName = MySchool","sa","sa"); System.out.println(conn); //3.执行sql语句,得到返回结果 String sql = "select * from emp"; //获得执行sql语句的对象 Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); //4.处理结果(游标,迭代指针) while(rs.next()){ int empno = rs.getInt("empno"); String ename = rs.getString("ename"); System.out.println(empno+ename); } //5.释放资源 rs.close(); stat.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2 . PreparedStatement比Statement提高了代码的可读性和可维护性,
提高了SQL语句执行的性能,提高了安全性
3. 核心API
DriverManager :管理驱动
Connection :数据库连接
Statement :执行sql语句
PreparedStatement :执行sql语句
execute(); :执行任何的sql
executeQuery (); :执行查询sql
executeUpdate (); :执行增删改sql
ResultSet
package day28; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class TestJDBC2 { public static void main(String[] args) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url ="jdbc:sqlserver://localhost:1433;databaseName = epet"; String username = "sa"; String password = "sa"; Connection conn = DriverManager.getConnection(url,username,password); int id = 1; String name = "刘春雷"; String money = "200"; String sql = "insert into master(id,name,money) values(?,?,?)"; PreparedStatement pst = conn.prepareStatement(sql); pst.setInt(1, id); pst.setString(2, name); pst.setString(3, money); pst.executeUpdate(); id = 2; name = "韩蝶儿"; money = "500"; pst.setInt(1, id); pst.setString(2, name); pst.setString(3, money); pst.executeUpdate(); id = 3; name = "程亚南"; money = "100"; pst.setInt(1, id); pst.setString(2, name); pst.setString(3, money); pst.executeUpdate(); pst.close(); conn.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }