JDBC操作(总结)
Posted 龙之天族
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC操作(总结)相关的知识,希望对你有一定的参考价值。
1.在项目中新建文件夹config(配置),在其中新建配置文件db.properties
driverName = oracle.jdbc.driver.OracleDriver url = jdbc:oracle:thin:@localhost:1521:orcl username = scott password = tiger
2.创建dbutil(数据库工具类)
(1)静态创建特性对象
static Properties properties = new Properties();
(2)在静态代码块中用特性对象读取相应的配置文件,并配置所需要的数据库类
static{ try { //加载数据库配置文件 properties.load(new FileInputStream("config/db.properties")); //2.载入JDBC驱动程序 Class.forName(properties.getProperty("driverName"));//驱动描述符 ,不能自定义 oracle.jdbc.driver.OracleDriver } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
(3)创建静态方法getConnection,用以得到数据库连接
public static Connection getConnection() { Connection conn = null; try { //3.定义连接URL String url = properties.getProperty("url");// 格式:jdbc:oracle:thin:@<主机名或IP>:1521:<数据库名> //4.建立连接 conn = DriverManager.getConnection(url, properties.getProperty("username"), properties.getProperty("password")); } catch (SQLException e) { e.printStackTrace(); } return conn; }
(4)创建静态方法close,用以关闭所用到的资源
public static void close(ResultSet rs,Statement stat,Connection conn) { //8.关闭连接(ResultSet,Statement,Connection) try { if(rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(stat != null) { stat.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } }
3.编写与数据库结构相同的类
public class Emp { private int empno; private String ename; private String job; private int mgr; private Date hiredate; private double salary; private double comm; private int deptno; public Emp() { } public Emp(int empno, String ename, String job, int mgr, Date hiredate, double salary, double comm, int deptno) { super(); this.empno = empno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.salary = salary; this.comm = comm; this.deptno = deptno; } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double getComm() { return comm; } public void setComm(double comm) { this.comm = comm; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate + ", salary=" + salary + ", comm=" + comm + ", deptno=" + deptno + "]"; } }
4.编写dao类,用以与数据库进行交互
(1)得到所有对象的方法
public List<Emp> getEmp() { try { //2~4 获取数据库连接 conn = DBUtil.getConnection(); //5.创建PreparedStatement对象 stat = conn.prepareStatement("select * from emp e"); //6.执行查询或更新 rs = stat.executeQuery(); //7.结果处理 List<Emp> emps = new ArrayList<Emp>(); while(rs.next()) { Emp emp = new Emp(); emp.setEmpno( rs.getInt("empno")); emp.setEname(rs.getString("ename")); emp.setSalary(rs.getDouble("sal")); //.......此处省略N个字段 //添加到集合 emps.add(emp); } return emps; } catch (SQLException e) { e.printStackTrace(); }finally{ //8.关闭连接(ResultSet,Statement,Connection) DBUtil.close(null, stat, conn); } return null; }
(2)插入数据的方法
public boolean insertEmp(Emp emp) { try { //2~4 获取数据库连接 conn = DBUtil.getConnection(); //5.创建PreparedStatement对象 stat = conn.prepareStatement("insert into emp (empno, ename) values (?, ?)"); //5.1绑定变量 stat.setInt(1, emp.getEmpno()); stat.setString(2, emp.getEname()); //6.执行更新 int result = stat.executeUpdate(); //7.结果处理 if(result > 0) { return true; } else { return false; } }catch (SQLException e) { e.printStackTrace(); }finally{ //8.关闭连接(ResultSet,Statement,Connection) DBUtil.close(rs, stat, conn); } return false; }
(3)改变数据的方法
public boolean updateEmp(Emp emp) { try { //2~4 获取数据库连接 conn = DBUtil.getConnection(); //5.创建PreparedStatement对象 stat = conn.prepareStatement("update emp set ename= ? where empno = ?"); //5.1绑定变量 stat.setString(1, emp.getEname()); stat.setInt(2, emp.getEmpno()); //6.执行更新 int result = stat.executeUpdate(); //7.结果处理 if(result > 0) { return true; } else { return false; } }catch (SQLException e) { e.printStackTrace(); }finally{ //8.关闭连接(ResultSet,Statement,Connection) DBUtil.close(null, stat, conn); } return false; }
(4)删除数据的方法
public boolean deleteEmp(int empno) { try { //2~4 获取数据库连接 conn = DBUtil.getConnection(); //5.创建PreparedStatement对象 stat = conn.prepareStatement("delete from emp where empno = ? "); //5.1绑定变量 stat.setInt(1, empno); //6.执行更新 int result = stat.executeUpdate(); //7.结果处理 if(result > 0) { return true; } else { return false; } } catch (SQLException e) { e.printStackTrace(); }finally{ //8.关闭连接(ResultSet,Statement,Connection) DBUtil.close(null, stat, conn); } return false; }
完成
注意:
1.获取申请时,一般获取的是prepareStatement,这样不会产生sql注入的危险(直接statement)
以上是关于JDBC操作(总结)的主要内容,如果未能解决你的问题,请参考以下文章
关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段