- jdbc
java api,由一套接口组成,提供了关系型数据库统一访问。
java.sql,javax.sql包
- 开发步骤
- DriverManager注册驱动
- 建立连接(jdbc)
- 发送sql给服务器
- 接收响应并处理
- 释放资源
- 入门案例
//1.注册驱动,DriverManager管理具体的驱动程序,实现对底层屏蔽,对开发人员提供统一访问。 //java.sql.Driver: jdbc的接口 //com.mysql.jdbc.Driver: mysql对jdbc的Driver接口实现类 DriverManager.registerDriver(new Driver()); //2.建立连接 java.sql.Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bd1807?useSSL=true", "root", "root"); //3.发送sql给数据库服务 String sql = "select * from userinfo"; //Statement对象:实现sql发送 stmt = conn.createStatement(); rs = stmt.executeQuery(sql); //4.处理响应结果(ResultSet底层维护了一个指向结果集的游标) while(rs.next()){ //向前移动游标(遍历一行) //获取游标指向行的字段的信息(遍历每列) int uid = rs.getInt("uid"); String uname = rs.getString("uname"); String password = rs.getString("password"); System.out.printf("uid:%s,uname:%s,password:%s\n",uid,uname,password); } |
- 常见API详解
- DriverManager(java.sql,类)
提供驱动的基础服务,对驱动实现细节进行屏蔽;
注册加载驱动;用于获取连接。
DriverManager.registerDriver(new Driver()); Class.forName("com.mysql.jdbc.Driver"); //建议使用第二种
conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bd1807?useSSL=true", "root", "root"); |
- Connection(java.sql 接口)
代表客户端程序与数据库服务的连接,与数据的交互都需要依赖于此连接。
CreateStatement:创建发送sql的statement对象 prepareStatement():创建预编译sql的preparedStatement对象 setAutocommit():设置事务自动提交 commit():手动提交事务 rollback():手动回滚事务 //获取连接的元数据信息 DatabaseMetaData md = conn.getMetaData(); System.out.println(md.getDatabaseProductName()) System.out.println(md.getDriverName()); System.out.println(md.getURL()); System.out.println(md.getUserName()); |
- Statement以及子接口(java.sql 接口)
发送sql命令
executeQuery():用于发送查询的sql语句 executeUpdate():用于发送增删改的sql语句 execute():用于发送任意的sql语句 addBatch():把多条sql语句添加到批处理命令中 execteBatch():用于执行批处理命令 |
- ResultSet(接口)
代表返回结果的结果集。底层维护游标,next()移动游标获取每一行数据。
Next():游标向前 getString()/getInt()/getObject():获取某行中某个字段值 //获取结果集的元数据 ResultSetMetaData md = rs.getMetaData(); //获取列 int count = md.getColumnCount(); //String name = md.getColumnName(column); //遍历获取每一个字段的名称 //类属性和表字段保持一致 //用于封装对象 |
- 增删改查
- statement存在sql注入风险(sql采用字符串拼接方式)
- PreparedStatement
Statement接口的子接口。
Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql:///bd1807", "root", "root"); //3.创建PreparedSatement发送sql String sql = "select * from userinfo where uname = ? and password = ?"; //预编译(语法语义检查) ps = conn.prepareStatement(sql); //赋值 ps.setString(1, uname); ps.setString(2, password);
rs = ps.executeQuery();
//4.处理结果 if(rs.next()){ System.out.println("登陆成功"); }else{ System.out.println("登录失败"); }
} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ try { if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } |
- dao开发
dao:data access object
在javaee分层开发中主要负责对数据库表操作的内容。
由接口和具体实现类组成。
接口声明规范;实现类可以有多个,对规范方法进行实现。
- 外部配置文件
import java.sql.*; import java.util.ArrayList; import java.util.List; public class JdbcDemo { public static void main(String[] args) { Userinfo info = new Userinfo(); info.setUname("www"); info.setPassword(1243); // insert(info); // delete(10); // demo1(); //login("ww","123"); login2("'or 1=1 or uname'",""); } //增加 public static void insert(Userinfo info){ Connection conn = null; Statement stmt = null; int rows=0; //1.注册驱动 try { Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 conn=DriverManager.getConnection( "jdbc:mysql://localhost:3306/bd1807?userSSL=true", "root", "root" ); //3.发送sql stmt = conn.createStatement(); String sql = "insert into userinfo(uname,password) values('"+info.getUname()+"',"+info.getPassword()+")"; stmt.executeUpdate(sql); rows= stmt.executeUpdate(sql); System.out.println(rows); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(stmt!=null){ stmt.close(); } if(conn!=null){ stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { } } } //删除 public static void delete(int num){ Connection conn = null; Statement stmt = null; int rows=0; //1.注册驱动 try { Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 conn=DriverManager.getConnection( "jdbc:mysql://localhost:3306/bd1807?userSSL=true", "root", "root" ); //3.发送sql stmt = conn.createStatement(); String sql = "delete from userinfo where uid ="+num; stmt.executeUpdate(sql); rows= stmt.executeUpdate(sql); System.out.println(rows); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(stmt!=null){ stmt.close(); } if(conn!=null){ stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { } } } //查询 public static void demo1() { //1.注册驱动,DriverManager管理具体的驱动程序,实现对底层的屏蔽,对开发人员提供统一的访问 //java.sql.Driver:jdbc的接口 //com.mysql.jdbc.Driver:mysql对jdbc的Driver接口的实现类 ResultSet rs=null; Connection conn=null; Statement stmt=null; List<Userinfo> list = new ArrayList<>(); try { // DriverManager.registerDriver(new Driver()); Class.forName("com.mysql.jdbc.Driver"); //2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bd1807?useSSL=true", "root", "root" ); //System.out.println(conn);检测是否连接成功 //3.发送sql给数据库的服务 String sql = "select * from userinfo"; //Statement对象:实现sql发送 stmt = conn.createStatement(); rs = stmt.executeQuery(sql); //4.处理响应的结果(Result底层维护了一个指向结果集的游标) while( rs.next()){//向前移动游标(遍历一行) int uid = rs.getInt("uid"); String uname = rs.getString("uname"); int password = rs.getInt("password"); Userinfo users = new Userinfo(); users.setUid(uid); users.setUname(uname); users.setPassword(password); list.add(users); // System.out.printf("uid:%s,uname:%s,password:%s\n",uid,uname,password); System.out.println(list); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if(rs!=null){ rs.close(); } if(stmt!=null){ stmt.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { } } } //登录 public static void login(String uname,String password){ ResultSet rs=null; Connection conn=null; Statement stmt=null; try { Class.forName("com.mysql.jdbc.Driver"); //2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bd1807?useSSL=true", "root", "root" ); //3.发送sql stmt = conn.createStatement(); String sql = "select * from userinfo where uname = '"+uname+"' and password = '"+password+"'"; rs = stmt.executeQuery(sql); //4.处理结果 if(rs.next()){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if(rs!=null){ rs.close(); } if(stmt!=null){ stmt.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } //防止sql注入的JDBC登录版本 public static void login2(String uname,String password){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql:///bd1807?useSSL=true",//"jdbc:mysql://localhost:3306/bd1807?useSSL=true" "root", "root" ); //3.创建PreparedSatement发送sql String sql = "select * from userinfo where uname = ? and password = ?"; //预编译 ps = conn.prepareStatement(sql); ps.setString(1,uname); ps.setString(2,password); rs = ps.executeQuery(); //4.处理结果 if(rs.next()){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { } } }