JDBC
jdbc.properties
driverClass=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb3?useSSL=false&serverTimezone=UTC name=root password=491183
JDBCUtil.java
1 import java.io.FileInputStream; 2 import java.io.InputStream; 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.Properties; 9 10 public class JDBCUtil { 11 12 static String driverClass = null; 13 static String url = null; 14 static String name = null; 15 static String password= null; 16 17 static{ 18 try { 19 //1. 创建一个属性配置对象 20 Properties properties = new Properties(); 21 InputStream is = new FileInputStream("jdbc.properties"); 22 23 //使用类加载器,去读取src底下的资源文件。 后面在servlet 24 // InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); 25 //导入输入流。 26 properties.load(is); 27 28 //读取属性 29 driverClass = properties.getProperty("driverClass"); 30 url = properties.getProperty("url"); 31 name = properties.getProperty("name"); 32 password = properties.getProperty("password"); 33 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 39 /** 40 * 获取连接对象 41 * @return 42 */ 43 public static Connection getConn(){ 44 Connection conn = null; 45 try { 46 Class.forName(driverClass); 47 //静态代码块 ---> 类加载了,就执行。 java.sql.DriverManager.registerDriver(new Driver()); 48 //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 49 //DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb"); 50 //2. 建立连接 参数一: 协议 + 访问的数据库 , 参数二: 用户名 , 参数三: 密码。 51 conn = DriverManager.getConnection(url, name, password); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 return conn; 56 } 57 58 /** 59 * 释放资源 60 * @param conn 61 * @param st 62 * @param rs 63 */ 64 public static void release(Connection conn , Statement st , ResultSet rs){ 65 closeRs(rs); 66 closeSt(st); 67 closeConn(conn); 68 } 69 70 71 private static void closeRs(ResultSet rs){ 72 try { 73 if(rs != null){ 74 rs.close(); 75 } 76 } catch (SQLException e) { 77 e.printStackTrace(); 78 }finally{ 79 rs = null; 80 } 81 } 82 83 private static void closeSt(Statement st){ 84 try { 85 if(st != null){ 86 st.close(); 87 } 88 } catch (SQLException e) { 89 e.printStackTrace(); 90 }finally{ 91 st = null; 92 } 93 } 94 95 private static void closeConn(Connection conn){ 96 try { 97 if(conn != null){ 98 conn.close(); 99 } 100 } catch (SQLException e) { 101 e.printStackTrace(); 102 }finally{ 103 conn = null; 104 } 105 } 106 }
UserDaoImpl.java
1 package com.itheima.dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import com.itheima.dao.UserDao; 10 import com.itheima.uitl.JDBCUtil; 11 12 public class UserDaoImpl implements UserDao{ 13 14 @Override 15 public void findAll() { 16 Connection conn = null; 17 Statement st = null; 18 ResultSet rs = null; 19 try { 20 //1. 获取连接对象 21 conn = JDBCUtil.getConn(); 22 //2. 创建statement对象 23 st = conn.createStatement(); 24 String sql = "select * from t_user"; 25 rs = st.executeQuery(sql); 26 27 while(rs.next()){ 28 String userName = rs.getString("username"); 29 String password = rs.getString("password"); 30 31 System.out.println(userName+"="+password); 32 } 33 34 } catch (Exception e) { 35 e.printStackTrace(); 36 }finally { 37 JDBCUtil.release(conn, st, rs); 38 } 39 } 40 41 /*@Override 42 public void login(String username, String password) { 43 44 Connection conn = null; 45 Statement st = null; 46 ResultSet rs = null; 47 try { 48 //1. 获取连接对象 49 conn = JDBCUtil.getConn(); 50 //2. 创建statement对象 51 st = conn.createStatement(); 52 // SELECT * FROM t_user WHERE username='admin' AND PASSWORD='10086' 53 String sql = "select * from t_user where username='"+ username +"' and password='"+ or +"'"; 54 rs = st.executeQuery(sql); 55 56 if(rs.next()){ 57 System.out.println("登录成功"); 58 }else{ 59 System.out.println("登录失败"); 60 } 61 62 } catch (Exception e) { 63 e.printStackTrace(); 64 }finally { 65 JDBCUtil.release(conn, st, rs); 66 } 67 }*/ 68 69 @Override 70 public void login(String username, String password) { 71 72 Connection conn = null; 73 Statement st = null; 74 ResultSet rs = null; 75 try { 76 //1. 获取连接对象 77 conn = JDBCUtil.getConn(); 78 //2. 创建statement对象 79 String sql = "select * from t_user where username=? and password=?"; 80 81 //预先对sql语句执行语法的校验, ? 对应的内容,后面不管传递什么进来,都把它看成是字符串。 or select 82 PreparedStatement ps = conn.prepareStatement(sql); 83 //? 对应的索引从 1 开始。 84 ps.setString(1, username); 85 ps.setString(2, password); 86 87 rs = ps.executeQuery(); 88 if(rs.next()){ 89 System.out.println("登录成功"); 90 }else{ 91 System.out.println("登录失败"); 92 } 93 94 } catch (Exception e) { 95 e.printStackTrace(); 96 }finally { 97 JDBCUtil.release(conn, st, rs); 98 } 99 } 100 101 102 103 104 @Override 105 public void insert(String userName, String password) { 106 Connection conn = null; 107 PreparedStatement ps = null; 108 109 try { 110 conn = JDBCUtil.getConn(); 111 String sql = "insert into t_user values(null , ? , ?)"; 112 ps = conn.prepareStatement(sql); 113 114 //给占位符赋值 从左到右数过来,1 代表第一个问号, 永远你是1开始。 115 ps.setString(1, userName); 116 ps.setString(2, password); 117 118 119 int result = ps.executeUpdate(); 120 if(result>0){ 121 System.out.println("添加成功"); 122 }else{ 123 System.out.println("添加失败"); 124 } 125 } catch (SQLException e) { 126 e.printStackTrace(); 127 }finally{ 128 JDBCUtil.release(conn, ps); 129 } 130 } 131 132 @Override 133 public void delete(int id) { 134 Connection conn = null; 135 PreparedStatement ps = null; 136 137 try { 138 conn = JDBCUtil.getConn(); 139 String sql = "delete from t_user where id = ?"; 140 ps = conn.prepareStatement(sql); 141 142 //给占位符赋值 从左到右数过来,1 代表第一个问号, 永远你是1开始。 143 ps.setInt(1, id); 144 145 146 int result = ps.executeUpdate(); 147 if(result>0){ 148 System.out.println("删除成功"); 149 }else{ 150 System.out.println("删除失败"); 151 } 152 } catch (SQLException e) { 153 e.printStackTrace(); 154 }finally{ 155 JDBCUtil.release(conn, ps); 156 } 157 } 158 159 @Override 160 public void update(int id, String name) { 161 Connection conn = null; 162 PreparedStatement ps = null; 163 164 try { 165 conn = JDBCUtil.getConn(); 166 String sql = "update t_user set username=? where id =?"; 167 ps = conn.prepareStatement(sql); 168 169 //给占位符赋值 从左到右数过来,1 代表第一个问号, 永远你是1开始。 170 ps.setString(1, name); 171 ps.setInt(2, id); 172 173 174 int result = ps.executeUpdate(); 175 if(result>0){ 176 System.out.println("更新成功"); 177 }else{ 178 System.out.println("更新失败"); 179 } 180 } catch (SQLException e) { 181 e.printStackTrace(); 182 }finally{ 183 JDBCUtil.release(conn, ps); 184 } 185 } 186 187 188 }
使用单元测试,测试代码
-
定义一个类, TestXXX , 里面定义方法 testXXX.
-
添加junit的支持。
右键工程 --- add Library --- Junit --- Junit4
-
在方法的上面加上注解 , 其实就是一个标记。
@Test public void testQuery() { ... }
-