1.概念
2.本质
3.好处
4.步骤
1.引入jar包
2.实例
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url="jdbc:mysql://127.0.0.1:3306/test";
- String userName="";
- String password="";
- Connection conn = DriverManager.getConnection(url, userName, password);
-
- //3.定义SQL
- String sql = "update account set money = 2000 where id = 1";
-
- //4.获取执行sql的对象Statement
- Statement stmt = conn.createStatement();
-
- //5.执行sql
- int count = stmt.executeUpdate(sql);//受影响的行数
-
- //6.处理结果
- System.out.println(count);
-
- //7.释放资源
- stmt.close();
- conn.close();
- }
1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
- static {
- try {
- DriverManager.registerDriver(new Driver());
- } catch (SQLException var1) {
- throw new RuntimeException("Can't register driver!");
- }
- }
2.获取连接
static Connection getConnection(String url, String user, String password)
Connection(数据库连接对象)作用
Statement createStatement()
PreparedStatement prepareStatement(String sql)
CallableStatement prepareCall(String sql)


Statement
statement 作用
- //执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。
- //执行DML,DDL语句
- //返回值:(1)DML语句影响的行数(2)DDL语句执行后,执行成功也可能返回0
- int executeUpdate(String sql)
- //执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。
- //返回值:ResultSet结果集对象
- ResultSet executeQuery(String sql)
- /**
- * 执行DML语句
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Test
- public void testDML() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url="jdbc:mysql://127.0.0.1:3306/test";
- String userName="";
- String password="";
- Connection conn = DriverManager.getConnection(url, userName, password);
-
- //3.定义SQL
- String sql = "update account set money = 2000 where id = 1";
-
- //4.获取执行sql的对象Statement
- Statement stmt = conn.createStatement();
-
- //5.执行sql
- int count = stmt.executeUpdate(sql);//受影响的行数
-
- //6.处理结果
- //System.out.println(count);
- if(count>0){
- System.out.println("修改成功");
- }else{
- System.out.println("修改失败");
- }
- //7.释放资源
- stmt.close();
- conn.close();
- }
- /**
- * 执行DDL语句
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Test
- public void testDDL() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url="jdbc:mysql://127.0.0.1:3306/test";
- String userName="";
- String password="";
- Connection conn = DriverManager.getConnection(url, userName, password);
-
- //3.定义SQL
- String sql = "drop database db1";
-
- //4.获取执行sql的对象Statement
- Statement stmt = conn.createStatement();
-
- //5.执行sql
- int count = stmt.executeUpdate(sql);//执行完DDL语句,可能是0
-
- //6.处理结果
- //System.out.println(count);
- // if(count>0){
- // System.out.println("修改成功");
- // }else{
- // System.out.println("修改失败");
- // }
- System.out.println(count);
- //7.释放资源
- stmt.close();
- conn.close();
- }
1.ResultSet(结果集对象)作用:
- ResultSet stmt.executeQuery(sql)
- //执行DQL语句,返回ResultSet对象
- //(1)将光标从当前位置向前移一行。(2)判断当前行是否为有效行
- /*
- 返回值:
- true:有效行,当前行有数据
- false:无效行,当前行没有数据
- */
- boolean next()
- xxx getXxx(参数):获取数据
- /*
- xxx:数据类型,如:int getInt(参数);String getString(参数)
- 参数:
- int:列的编号,从1开始
- String:列的名称
- */
2.使用步骤
- //循环判断游标是否是最后一行
- While(rs.next()){
- //获取数据
- rs.getXxx(参数);
- }
- /**
- * 执行DQl语句
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Test
- public void testResultSet() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url="jdbc:mysql://127.0.0.1:3306/test";
- String userName="";
- String password="";
- Connection conn = DriverManager.getConnection(url, userName, password);
- //3.定义SQL
- String sql = "select * from account";
-
- //4.获取statement对象
- Statement stmt = conn.createStatement();
-
- //5.执行sql
- ResultSet rs = stmt.executeQuery(sql);
-
- // //6.处理结果
- // //光标向下移动一行,并且判断当前行是否有数据
- // while(rs.next()){
- // //获取数据 getXcc()
- // int id = rs.getInt(1);
- // String name = rs.getString(2);
- // double money = rs.getDouble(3);
- //
- // System.out.println(id);
- // System.out.println(name);
- // System.out.println(money);
- //
- // System.out.println("---------------");
- // }
- //6.处理结果
- //光标向下移动一行,并且判断当前行是否有数据
- while(rs.next()){
- //获取数据 getXcc()
- int id = rs.getInt("id");
- String name = rs.getString("name");
- double money = rs.getDouble("money");
-
- System.out.println(id);
- System.out.println(name);
- System.out.println(money);
-
- System.out.println("---------------");
- }
-
- //7.释放资源
- rs.close();
- stmt.close();
- conn.close();
- }
3.案例
- /**
- * 查询account账户表数据,封装为Account对象中,并且存储到ArrayList集合中
- * 1.定义实体类Account
- * 2.查询数据,封装到Account对象中
- * 3.将Account对象存入ArrayList集合中
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Test
- public void testResultSet2() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url="jdbc:mysql://127.0.0.1:3306/test";
- String userName="";
- String password="";
- Connection conn = DriverManager.getConnection(url, userName, password);
- //3.定义SQL
- String sql = "select * from account";
-
- //4.获取statement对象
- Statement stmt = conn.createStatement();
-
- //5.执行sql
- ResultSet rs = stmt.executeQuery(sql);
-
- //创建集合
- ArrayList
list = new ArrayList<>(); - //6.处理结果
- //光标向下移动一行,并且判断当前行是否有数据
- while(rs.next()){
- Account account = new Account();
-
- //获取数据 getXcc()
- int id = rs.getInt("id");
- String name = rs.getString("name");
- double money = rs.getDouble("money");
-
- System.out.println(id);
- System.out.println(name);
- System.out.println(money);
-
- //赋值
- list.add(account);
- }
-
- System.out.println(list);
-
- //7.释放资源
- rs.close();
- stmt.close();
- conn.close();
- }
1.作用
- /**
- * 演示SQL注入
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Test
- public void testLogin_Inject() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url = "jdbc:mysql://127.0.0.1:3306/test";
- String userName = "";
- String password = "";
- Connection conn = DriverManager.getConnection(url, userName, password);
-
- //接受用户输入的用户名和密码
- String name = "fghfgh";
- String pwd = "' or '1' = '1";
- //定义SQL
- String sql = "select * from tb_user where username = '"+name+"' and password = '"+pwd+"'";
- System.out.println(sql);
- //获取statement对象
- Statement stmt = conn.createStatement();
-
- //执行sql
- ResultSet rs = stmt.executeQuery(sql);
-
- //判断登录是否成功
- if(rs.next()){
- System.out.println("登录成功");
- }else {
- System.out.println("登录失败");
- }
-
- //释放资源
- rs.close();
- stmt.close();
- conn.close();
- }
2.案例
select * from tb_user where username='zhangsan' and password='123' ;
- /**
- * 用户登录
- *
- * @throws ClassNotFoundException
- * @throws SQLException
- */
- @Test
- public void testLogin() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url = "jdbc:mysql://127.0.0.1:3306/test";
- String userName = "";
- String password = "";
- Connection conn = DriverManager.getConnection(url, userName, password);
-
- //接受用户输入的用户名和密码
- String name = "zhangsan";
- String pwd = "123";
- //定义SQL
- String sql = "select * from tb_user where username = '"+name+"' and password = '"+pwd+"'";
-
- //获取statement对象
- Statement stmt = conn.createStatement();
-
- //执行sql
- ResultSet rs = stmt.executeQuery(sql);
-
- //判断登录是否成功
- if(rs.next()){
- System.out.println("登录成功");
- }else {
- System.out.println("登录失败");
- }
-
- //释放资源
- rs.close();
- stmt.close();
- conn.close();
- }
3.步骤
- //SQL语句中的参数值,使用?占位符替代
- String sql = "select * from tb_user where username = ? and password = ?";
- //通过Connection对象获取,并传入对应的sql语句
- PrepareStatement pstmt = conn.prepareStatement(sql);
PrepareStatement对象:setXxx(参数1,参数2) 给?赋值
Xxx:数据类型;如setInt(参数1,参数2)
参数:
参数1:?的位置编号,从1开始
参数2:?的值
- executeUpdate();/executeQuery();
- 不需要传递参数
- @Test
- public void testLogin() throws ClassNotFoundException, SQLException {
- //1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
-
- //2.获取连接
- String url = "jdbc:mysql://127.0.0.1:3306/test";
- String userName = "";
- String password = "";
- Connection conn = DriverManager.getConnection(url, userName, password);
-
- //接受用户输入的用户名和密码
- String name = "zhangsan";
- String pwd = "123";
- //定义SQL
- String sql = "select * from tb_user where username = ? and password = ?";
-
- //获取statement对象
- PreparedStatement pstmt = conn.prepareStatement(sql);
-
-
- //设置?的值
- pstmt.setString(1,name);
- pstmt.setString(2,pwd);
-
- //执行sql
- ResultSet rs = pstmt.executeQuery();
-
- //判断登录是否成功
- if(rs.next()){
- System.out.println("登录成功");
- }else {
- System.out.println("登录失败");
- }
-
- //释放资源
- rs.close();
- pstmt.close();
- conn.close();
- }
4.原理

1.概念
2.实现
Connection getConnection()
3.Druid 使用步骤
- public static void main(String[] args) throws Exception {
- //1. 导入jar包druid
- //2. 定义配置文件
- //3. 加载配置文件
- Properties prop = new Properties();
- prop.load(new FileInputStream("src/main/java/com/example/javaWeb/druid.properties"));
- //4. 获取数据库连接池对象
- DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
- //5. 获取连接
- Connection connection = dataSource.getConnection();
-
- System.out.println(connection);
-
- // System.out.println(System.getProperty("user.dir"));
- }
1.增删改查
- /**
- * 查询所有
- */
- @Test
- public void testSelectAll() throws Exception {
- Properties prop = new Properties();
- prop.load(new FileInputStream("src/main/java/com/example/javaWeb/druid.properties"));
- //4. 获取数据库连接池对象
- DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
- //5. 获取连接
- Connection conn = dataSource.getConnection();
-
- //定义SQL
- String sql = "select* from tb_brand;";
-
- //获取pstmt对象
- PreparedStatement pstmt = conn.prepareStatement(sql);
-
- //执行sql
- ResultSet rs = pstmt.executeQuery();
-
- //处理结果
- Brand brand = null;
- ArrayList
brands = new ArrayList<>(); - while(rs.next()){
- //获取数据
- int id = rs.getInt("id");
- String brandName = rs.getString("brand_name");
- String companyName = rs.getString("company_name");
- int ordered = rs.getInt("ordered");
- String description =rs.getString("description");
- int status = rs.getInt("status");
-
- //封装Brand对象
- brand = new Brand();
- brand.setId(id);
- brand.setBrandName(brandName);
- brand.setCompanyName(companyName);
- brand.setOrdered(String.valueOf(ordered));
- brand.setDescription(description);
- brand.setStatus(status);
-
- //装在对象
- brands.add(brand);
- }
- System.out.println(brands);
- //释放资源
- rs.close();
- pstmt.close();
- conn.close();
- }
- /**
- * 添加
- */
- @Test
- public void testAdd() throws Exception {
- //接受页面提交的参数
- String brandName = "香飘飘";
- String companyName = "香飘飘";
- int ordered = 1;
- String description = "绕地球一周";
- int status = 1;
- //int id = 4; //修改数据库
- Properties prop = new Properties();
- prop.load(new FileInputStream("src/main/java/com/example/javaWeb/druid.properties"));
- //4. 获取数据库连接池对象
- DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
- //5. 获取连接
- Connection conn = dataSource.getConnection();
-
- //定义SQL
- String sql = "INSERT INTO tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?);";
- //修改数据
- //String sql = "update tb_brand set brand_name = ?,company_name=?,ordered = ?,description=?,status=?where id = ?;"
- //删除数据
- //String sql = "delete from tb_brand where id = ?"
- //获取pstmt对象
- PreparedStatement pstmt = conn.prepareStatement(sql);
-
- //设置参数
- pstmt.setString(1, brandName);
- pstmt.setString(2, companyName);
- pstmt.setInt(3, ordered);
- pstmt.setString(4, description);
- pstmt.setInt(5, status);
- //pstmt.setInt(6,id); //修改数据
- //执行sql
- int count = pstmt.executeUpdate();
-
- //处理结果
- System.out.println(count > 0);
- //释放资源
-
- pstmt.close();
- conn.close();
- }