当前位置:Gxlcms > 数据库问题 > JAVA11-JDBC学习

JAVA11-JDBC学习

时间:2021-07-01 10:21:17 帮助过:4人阅读

package cn.itcast.demo01.demo01.demo03; 2 3 import java.sql.Connection; 4 import java.sql.Driver; 5 import java.sql.DriverManager; 6 import java.sql.Statement; 7 8 public class JdbcDemo01 { 9 public static void main(String[] args) throws Exception { 10 //导入驱动jar包 11 //注册驱动 12 Class.forName("com.mysql.jdbc.Driver"); 13 //获取数据库连接对象 connection 14 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root"); 15 // 定义sql语句 16 String sql = "update emp set salary = 7500 where id = 1001;"; 17 // 获取执行sql的对象,statement 18 Statement stmt = conn.createStatement(); 19 //执行sql 20 int count = stmt.executeUpdate(sql);// 返回的是影响的行数 21 // 处理结果 22 System.out.println(count); 23 //释放资源 24 stmt.close(); 25 conn.close(); 26 } 27 }

2.JDBC的各个类

技术图片


  1 package cn.itcast.demo01.demo01.demo03;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.SQLException;
  6 import java.sql.Statement;
  7 
  8 public class JdbcDemo02 {
  9     public static void main(String[] args) {
 10         Statement stmt = null;
 11         Connection conn = null;
 12         try {// 注册驱动
 13             Class.forName("com.mysql.jdbc.Driver");
 14             //定义sql
 15             String sql = "insert into account values (null,‘王五‘,3500)";
 16             // 获取Connection对象
 17             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
 18 
 19             // 获取执行sql的对象 Statement
 20             stmt = conn.createStatement();
 21             // 执行sql
 22             int count = stmt.executeUpdate(sql);// 影响的行数
 23             System.out.println(count);
 24             if(count > 0){
 25                 System.out.println("执行成功");
 26 
 27             }else{
 28                 System.out.println("执行失败");
 29             }
 30         } catch (ClassNotFoundException | SQLException e) {
 31             e.printStackTrace();
 32         } finally {
 33             if(stmt != null){
 34                 try {
 35                     stmt.close();
 36                 } catch (SQLException e) {
 37                     e.printStackTrace();
 38                 }
 39             }
 40             if(conn != null){
 41                 try {
 42                     stmt.close();
 43                 } catch (SQLException e) {
 44                     e.printStackTrace();
 45                 }
 46             }
 47         }
 48     }
 49 }
 50 

ResultSet接口,返回值是结果集,常见的方法如下:

  • next();方法 游标向下移动一行,默认指向表头;
  • getXxx(参数);一次获取一行某一列的数据,

其中xxx代表数据类型,如int getInt();String类型 getString();

     参数 int 代表列的变化,从1开始,如getString(1);

           string 代表列的名称 ,如 getDouble(“balance”)

  1 package cn.itcast.demo01.demo01.demo03;
  2 
  3 import java.sql.*;
  4 
  5 public class JdbcDemo02 {
  6     public static void main(String[] args) {
  7         Statement stmt = null;
  8         Connection conn = null;
  9         ResultSet rs = null;
 10         try {// 注册驱动
 11             Class.forName("com.mysql.jdbc.Driver");
 12             //定义sql
 13             String sql = "select * from account";
 14             // 获取Connection对象
 15             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
 16 
 17             // 获取执行sql的对象 Statement
 18             stmt = conn.createStatement();
 19             // 执行sql
 20             rs = stmt.executeQuery(sql);// 获取结果集
 21             rs.next();
 22             int id = rs.getInt(1);
 23             String name = rs.getString("name");
 24             double balance = rs.getDouble(3);
 25             System.out.println(name+balance);
 26 
 27 
 28         } catch (ClassNotFoundException | SQLException e) {
 29             e.printStackTrace();
 30         } finally {
 31             if(rs != null){
 32                 try {
 33                     stmt.close();
 34                 } catch (SQLException e) {
 35                     e.printStackTrace();
 36                 }
 37             }
 38             if(stmt != null){
 39                 try {
 40                     stmt.close();
 41                 } catch (SQLException e) {
 42                     e.printStackTrace();
 43                 }
 44             }
 45             if(conn != null){
 46                 try {
 47                     stmt.close();
 48                 } catch (SQLException e) {
 49                     e.printStackTrace();
 50                 }
 51             }
 52         }
 53     }
 54 }
 55 

        使用ResultSet正确用法: 游标向下移动一行,判断有无数据,再行获取数据,可使用boolean next();方法含义:判断当前行是否是最后一行结尾,如果是,则返回false,如果不是则返回true;或者使用while(rs.next()){}

练习:查询emp表的数据,将其封装为对象。然后装载集合,返回emp表包含对象。类似于将某一行作为一个对象,因为每一行都有变量名和类型。用集合将对象装载。

  • 首先  定义Emp类
  • 其次  定义方法 public List<Emp> findAll(){}
  • 最后  实现方法  select *from emp;
  1 package cn.itcast.demo01.demo01.demo03;
  2 
  3 import cn.itcast.demo01.demo01.demo04;
  4 
  5 import java.sql.*;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 public class JdbcDemo04 {
 10     /*
 11 查询所有emp对校
 12  */
 13     public static void main(String[] args) {
 14         List<demo04> list = new JdbcDemo04().findAll();
 15         System.out.println(list);
 16     }
 17     public List<demo04> findAll(){
 18         ResultSet rs = null;
 19         Statement stmt = null;
 20         Connection conn = null;
 21         List<demo04> list = null;
 22         try {
 23             Class.forName("com.mysql.jdbc.Driver");
 24             // 获取连接
 25             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
 26             //  定义sql
 27             String sql = "select * from emp";
 28             // 获取执行sql 的对校
 29             stmt = conn.createStatement();
 30             // 执行sql
 31             rs = stmt.executeQuery(sql);
 32             demo04 emp = null;// 先创建一个对象
 33             list = new ArrayList<demo04>();
 34             while ( rs.next()){
 35                 int id = rs.getInt("id");
 36                 String ename = rs.getString("ename");
 37                 int job_id = rs.getInt("job_id");
 38                 int mgr = rs.getInt("mgr");
 39                 Date joindate = rs.getDate("joindate");
 40                 double salary = rs.getDouble("salary");
 41                 double bonus = rs.getDouble("bonus");
 42                 int dept_id = rs.getInt("dept_id");
 43                 emp = new demo04();// 创建emp对象,并赋值
 44                 emp.setId(id);
 45                 emp.setEname(ename);
 46                 emp.setJob_id(job_id);
 47                 emp.setMgr(mgr);
 48                 emp.setJoindate(joindate);
 49                 emp.setSalary(salary);
 50                 emp.setBonus(bonus);
 51                 emp.setDept_id(dept_id);
 52                 list.add(emp);
 53             }
 54         } catch (ClassNotFoundException e) {
 55             e.printStackTrace();
 56         } catch (SQLException e) {
 57             e.printStackTrace();
 58         }finally {
 59             if (rs != null){
 60                 try {
 61                     rs.close();
 62                 } catch (SQLException e) {
 63                     e.printStackTrace();
 64                 }
 65             }
 66             if (stmt != null){
 67                 try {
 68                     rs.close();
 69                 } catch (SQLException e) {
 70                     e.printStackTrace();
 71                 }
 72             }
 73             if (conn != null){
 74                 try {
 75                     rs.close();
 76                 } catch (SQLException e) {
 77                     e.printStackTrace();
 78                 }
 79             }
 80 
 81         }
 82         return  list;
 83 
 84 
 85     }
 86 }
 87 

上图中,注册驱动和释放资源代码重复性较高,因此有必要抽取JDBC工具类

即可:JDBCUtils 


JAVA11-JDBC学习

标签:oid   端口   字符串   throws   manager   gis   目的   发送   撤销   

人气教程排行