Jdbc
1.什么是jdbc?
使用java代码(程序)发送sql语句的技术,就是jdbc技术。
2.使用jdbc编程六步
1.注册驱动
告诉java程序,即将要链接的数据库是哪个品牌
2.获取链接
表示jvm的进程和数据库进程之间的通道打开了,这属于进程之间的通讯,使用完之后一定要关闭资源
3.获取数据库操作对象
专门执行SQL语句
4.执行SQL语句
DQL,DML……
5.处理查询结果集
只有当第四步执行的是select语句的时候,才有的这第五步处理查询结果集
6.释放资源
使用完资源之后一定要关闭资源,java和数据库属于进程间的通讯,开启之后,一定要关闭
3.JDBC接口核心的API
java.sql.* 和 javax.sql.*
Driver接口:表示java驱动程序接口。所有具体的数据库厂商要来实现此接口。
connect(url, properties):链接数据库的方法。
url:链接数据库的URL
URL语法:jdbc协议 : 数据库子协议 : // 主机 : 端口 / 数据库
user:数据库的用户名
password:数据库用户密码
DriverManager类:驱动管理类,用于管理所有注册的驱动程序
registerDriver(driver):注册驱动类对象
Connection getConnection(url, user, password):获取链接对象
Connection接口:表示java程序和数据库的链接对象
Statement createStatement() :创建 Statement 对象
PrepardStatement prepareStatement(String sql) 创建PreparedStatement 对象
CallableStatement prepareCall(String sql) 创建CallableStatement对象
Statement接口:用于执行静态的sql语句
int executeUpdate(String sql) :执行静态的更新sql语句(DDL, DML)
ResultSet executeQuery(String sql) :执行的静态的查询sql语句(DQL)
PreparedStatement接口:用于执行预编译sql语句
int executeUpdate() :执行预编译的更新sql语句(DDL, DML)
ResultSet executeQuery() :执行预编译的查询sql语句(DQL)
CallableStatement接口:用于执行存储过程的sql语句(call xxx)
ResultSet executeQuery() :调用存储过程的方法
ResultSet接口:用于封装查询出来的数据
boolean next() :将光标移动到下一行
getXX() :获取列的值
简单链接数据库
import org.junit.Test; import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.Properties; public class jdbcDome1Test { //数据库用户名 private static String user = "root"; //数据库密码 private static String password = "0000"; //链接数据库的URL //URL语法:jdbc协议 : 数据库子协议 : // 主机 : 端口 / 数据库 private static String url = "jdbc:mysql://localhost:3306/jdbcdata"; @Test //测试方法,相当于main方法 public void Test1(){ try { //获取Driver(驱动) Driver driver = new com.mysql.jdbc.Driver(); //创建properties 数据类型,数据以键值对的方式存储 Properties properties = new Properties(); properties.setProperty("user", user); properties.setProperty("password", password); //创建数据库链接对象 Connection connect = driver.connect(url, properties); //测试数据库是否链接上 System.out.println(connect); } catch (SQLException e) { e.printStackTrace(); } } }
工具类
import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class jdbcUtil { //数据库用户名 private static String username; //密码 private static String password; //地址 private static String url; //驱动 private static String driverClassName; //静态代码块 static { try { //创建读取流,读取db.properties文件 InputStream resourceAsStream = jdbcUtil.class.getResourceAsStream("/db.properties"); //加载db.properties配置文件 Properties properties = new Properties(); //加载 properties.load(resourceAsStream); //赋值 username = properties.getProperty("username"); password = properties.getProperty("password"); url = properties.getProperty("url"); driverClassName = properties.getProperty("driverClassName"); //注册驱动 Class.forName(driverClassName); } catch (Exception e) { e.printStackTrace(); } } /** * 创建获取 数据库链接对象 方法 * @return 数据库链接对象 */ public static Connection getConnection(){ try { //获取链接对象 Connection connection = DriverManager.getConnection(url, username, password); return connection; } catch (SQLException e) { e.printStackTrace(); } throw new RuntimeException("数据库链接失败"); } }
配置文件db.porperties
username=root password=0000 url=jdbc:mysql://localhost:3306/jdbcdata driverClassName=com.mysql.jdbc.Driver
执行sql语句
import org.junit.Test; import utils.jdbcUtil; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class jdbcDome2 { @Test public void Test2(){ //执行删除SQL语句executeUpdate(String sql) // try { // //获取链接 // Connection connection = jdbcUtil.getConnection(); //// System.out.println(connection); // //删除操作:delete from Student where id = 3 // //获取静态Sql对象Statement // Statement statement = connection.createStatement(); // // //准备 需要执行的 Sql 语句 // String str = "delete from Student where id = 3 "; // // int i = statement.executeUpdate(str); // System.out.println(i + "条语句受到了影响"); // // //关闭流 // statement.close(); // connection.close(); // // } catch (SQLException e) { // e.printStackTrace(); // } //执行添加SQL语句executeUpdate(String sql) // try { // //获取链接 // Connection connection = jdbcUtil.getConnection(); //// System.out.println(connection); // // //获取静态Sql对象Statement // Statement statement = connection.createStatement(); // // //准备 需要执行的 Sql 语句 // int id = 2; // String name = "张三"; // int age = 23; // String work = "搬运工"; // String str = "insert into emp(id,name,age,work) values" + // "(" + id + "," + "'"+ name + "'" + "," + age + "," +"'"+ work +"'"+ ")"; // //// System.out.println(str); // //添加操作: // int i = statement.executeUpdate(str); // System.out.println(i + "条语句受到了影响"); // // //关闭流 // statement.close(); // connection.close(); // // } catch (SQLException e) { // e.printStackTrace(); // } //执行添加SQL语句executeUpdate(String sql) // try { // //获取链接 // Connection connection = jdbcUtil.getConnection(); //// System.out.println(connection); // // //获取静态Sql对象Statement // Statement statement = connection.createStatement(); // // //准备 需要执行的 Sql 语句 // String name = "张三"; // int id = 2; // String str = "update Student set name="+"'"+name+"'" + "where id ="+id; // // //修改Student表中的数据 // int i = statement.executeUpdate(str); // System.out.println(i + "条语句受到了影响"); // // //关闭流 // statement.close(); // connection.close(); // // } catch (SQLException e) { // e.printStackTrace(); // } //执行查询SQL语句executeQuery(String sql) // try { // //获取链接 // Connection connection = jdbcUtil.getConnection(); //// System.out.println(connection); // // //获取静态Sql对象Statement // Statement statement = connection.createStatement(); // // //准备 需要执行的 Sql 语句 // // //查询id为4的人名字,年龄,性别 // String str = "select name, age, sex from Student where id = 4"; // // //执行查询 // ResultSet resultSet = statement.executeQuery(str); // // //光标向下移动一行(false:表示表中没有数据) //// boolean next = resultSet.next(); // // resultSet.next(); // //获取数据(使用列名获取) //// String name = resultSet.getString("name"); //// int age = resultSet.getInt("age"); //// String sex = resultSet.getString("sex"); //// System.out.println(name + "---" + age + "---" + sex); // // //获取数据(使用索引 --> 注意:从 1 开始) // String name = resultSet.getString(1); // int age = resultSet.getInt(2); // String sex = resultSet.getString(3); // System.out.println(name + "---" + age + "---" + sex); // // //关闭流 // statement.close(); // connection.close(); // // } catch (SQLException e) { // e.printStackTrace(); // } //执行查询SQL语句executeQuery(String sql) try { //获取链接 Connection connection = jdbcUtil.getConnection(); // System.out.println(connection); //获取静态Sql对象Statement Statement statement = connection.createStatement(); //准备 需要执行的 Sql 语句 //查询所有的人名字,年龄,性别 String str = "select name, age, sex from Student"; //执行查询 ResultSet resultSet = statement.executeQuery(str); //光标向下移动一行(false:表示表中没有数据) // boolean next = resultSet.next(); //获取全部数据(使用列名获取) while (resultSet.next()) { String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String sex = resultSet.getString("sex"); System.out.println(name + "---" + age + "---" + sex); } //关闭流 statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
PreparedStatement vs Statement
1)语法不同:PreparedStatement可以使用预编译的sql,而Statement只能使用静态的sql。
2)效率不同:PreparedStatement可以使用sql缓冲区,效率不Statement高
3)安全性不同:PreparedStatement可以有效防止sql注入,而Statement不能防止sql注入。
推荐使用PreparedStatement
使用PreparedStatement
import org.junit.Test; import utils.jdbcUtil; import java.sql.*; public class jdbcDome3 { @Test public void Test1() throws Exception { //获取链接 Connection connection = jdbcUtil.getConnection(); //准备SQL String sql = "delete from emp where id = ?"; //获取预编译SQL对象preparedStatement PreparedStatement preparedStatement = connection.prepareStatement(sql); //设置参数值 preparedStatement.setInt(1,2); //执行sql int i = preparedStatement.executeUpdate(); System.out.println(i + "条数据受到影响"); //关闭流 preparedStatement.close(); connection.close(); } @Test public void Test2() throws SQLException { //获取链接 Connection connection = jdbcUtil.getConnection(); //准备SQL String sql = "insert into emp (name, age, work) values(?, ?, ?)"; //获取预编译SQL对象,preparedStatement PreparedStatement preparedStatement = connection.prepareStatement(sql); //设置参数值 preparedStatement.setString(1, "皮卡丘"); preparedStatement.setInt(2,23); preparedStatement.setString(3,"斗地主"); //执行sql int i = preparedStatement.executeUpdate(); System.out.println(i + "条数据受到影响"); preparedStatement.close(); connection.close(); } @Test public void Test3() throws SQLException { //获取链接 Connection connection = jdbcUtil.getConnection(); //准备SQL String sql = "update emp set name = ?, Age = ? where id = 5"; //获取预编译SQL对象,preparedStatement PreparedStatement preparedStatement = connection.prepareStatement(sql); //设置参数值 preparedStatement.setString(1, "干啥"); preparedStatement.setInt(2,20); //执行sql int i = preparedStatement.executeUpdate(); System.out.println(i + "条数据受到影响"); preparedStatement.close(); connection.close(); } @Test public void Test4() throws SQLException { //获取链接 Connection connection = jdbcUtil.getConnection(); //准备SQL语句 String sql = "select * from emp"; //获取预编译SQL对象,preparedStatement PreparedStatement preparedStatement = connection.prepareStatement(sql); //运行SQL语句 ResultSet resultSet = preparedStatement.executeQuery(); //输出语句 while (resultSet.next()){ int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); String word = resultSet.getString("work"); System.out.println("id" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "工作"); System.out.println(id + "\t" + name + "\t" + age + "\t" + word); } //关闭流 resultSet.close(); preparedStatement.close(); connection.close(); } }
PreparedStatement防止注入
import org.junit.Test; import utils.jdbcUtil; import java.sql.*; //使用预编译sql可以防止注入 public class jdbcDome4 { @Test public void Test1() throws Exception { //模拟页面数据 // String username = "张三"; //用户名 // --在SQL中表示注释 // 李四 or 2=2 -- 注意--后面要加空格 String username = "jack222' or 1=1 -- "; String password = "1233423423423"; //密码 始终为错误密码 //获取链接 Connection connection = jdbcUtil.getConnection(); //准备sql //查询判断用户名和密码是否正确,如果查询结果有内容表示账号密码正确 String sql = "select * from t_user where name = '"+username+"' and password = '"+password+"'"; //获取执行SQL对象 Statement statement = connection.createStatement(); //执行SQL语句 ResultSet resultSet = statement.executeQuery(sql); //判断查询出的表格是否有内容 if(resultSet.next()){ System.out.println("true"); }else{ System.out.println("false"); } //原理:将SQL注释代码当做用户名注入到SQL语句中,注释掉后面的比较密码语句 } //PreparedStatement有效的防止sql注入 @Test public void Test2() throws SQLException { //模拟页面数据 // String username = "张三"; //用户名 // --在SQL中表示注释 // 李四 or 2=2 -- 注意--后面要加空格 String username = "jack222' or 1=1 -- "; String password = "1233423423423"; //密码 始终为错误密码 //获取链接 //查询判断用户名和密码是否正确,如果查询结果有内容表示账号密码正确 Connection connection = jdbcUtil.getConnection(); //准备SQL String sql = "select * from t_user where name = ? and password = ?"; //获取预编译SQL对象 PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, username); preparedStatement.setString(2, password); //执行 ResultSet resultSet = preparedStatement.executeQuery(); //判断查询出的表格是否有内容 if (resultSet.next()){ System.out.println(true); }else { System.out.println(false); } } }

浙公网安备 33010602011771号