package com.jdbc02; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { Connection cc = null; PreparedStatement ps = null; ResultSet rs = null; private static final String driver = "com.mysql.cj.jdbc.Driver"; private final String url = "jdbc:mysql://127.0.0.1:3306/class?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC"; private final String user = "root"; private static final String password = "emp2014ljh"; //获取数据库连接 public void getConnection() { try { Class.forName(driver); cc = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //查询方法 public ResultSet executeQuery(String sql ,Object... obj) { getConnection(); try { ps = cc.prepareStatement(sql); if(obj != null) { for(int i = 0;i < obj.length;i++) { ps.setObject((i+1), obj[i]); } } rs = ps.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } //跟新方法(增加删除修改) public int executeUpdate(String sql,Object... obj) { getConnection(); int result = 0; try { ps = cc.prepareStatement(sql); if(obj != null) { for(int i = 0;i < obj.length;i++) { ps.setObject((i+1), obj[i]); } } result = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { closeAll(); } return result; } //释放资源 public void closeAll() { try { if(rs != null) { rs.close(); } if (ps!=null){ ps.close(); } if (cc!=null){ cc.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.test; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import com.jdbc02.BaseDao; public class jdbc_test { @Test //查询全部学生信息 public void test01() { BaseDao bd = new BaseDao(); Object[] obj = {}; ResultSet rs = bd.executeQuery("SELECT * FROM STUDENT",obj); try { while(rs.next()) { System.out.println(rs.getObject(1).toString()+" "+rs.getObject(2).toString()+" "+rs.getObject(3).toString()+" " +rs.getObject(4).toString()+" "+rs.getObject(5).toString()+" "+rs.getObject(6).toString()+" "+rs.getObject(7).toString() +" "+rs.getObject(8).toString()); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } bd.closeAll(); } @Test //查询某个的全部信息 public void test02() { BaseDao bd = new BaseDao(); String sql ="SELECT * FROM STUDENT where id = ?" ; Object[] obj = {2}; ResultSet rs = bd.executeQuery(sql, obj); try { while(rs.next()) { System.out.println(rs.getObject(1).toString()+" "+rs.getObject(2).toString()+" "+rs.getObject(3).toString()+" " +rs.getObject(4).toString()+" "+rs.getObject(5).toString()+" "+rs.getObject(6).toString()+" "+rs.getObject(7).toString() +" "+rs.getObject(8).toString()); } bd.closeAll(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void test03() { BaseDao bd = new BaseDao(); String sql ="SELECT id,username,phone,address FROM student where usercode=?;" ; Object[] obj = {"guapi"}; ResultSet rs = bd.executeQuery(sql, obj); try { while(rs.next()) { System.out.println(rs.getObject("id")+" "+rs.getObject(2)+" "+rs.getObject(3)); } bd.closeAll(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } bd.closeAll(); } @Test //添加信息 public void test04() { BaseDao bd = new BaseDao(); String sql = "INSERT INTO student VALUES(?,?,?,?,?,?,?,?);"; Object[] obj = {4,"guapi","瓜皮","123456",1,"15350593845","1997-03-30","北京市"}; int result = bd.executeUpdate(sql, obj); if(result >0) { System.out.println("添加成功"); }else { System.out.println("添加失败"); } } @Test //删除信息 public void test05() { BaseDao bd = new BaseDao(); String sql = "DELETE FROM student where id=?"; Object[] obj = {5}; int a = bd.executeUpdate(sql, obj); if(a >0) { System.out.println("删除成功"); }else { System.out.println("删除失败"); } } //跟新信息 @Test public void Test06() { BaseDao bd = new BaseDao(); String sql = "update student SET `password`='654321' WHERE id = ?;"; Object[] obj = {4}; int b = bd.executeUpdate(sql, obj); if(b>0) { System.out.println("修改成功"); }else { System.out.println("修改失败"); } } }
测试类添加修改类