一,enum类型的使用
在SQL中没有布尔类型的数据,我们都使用过布尔类型,当属性的值只用两种情况时。例如性别等。那在数据库对这些属性的值个数比较少时我们应该使用什么数据类型呢?SQL给我们提供了枚举类型可以使用。例如:gender enum('男','女') not null;
SQL文件:(之后也是用该表)注意:本文章使用的数据库是mysql,版本是mysql8.0.18.
SHOW DATABASES; USE student; CREATE TABLE stu_message( id BIGINT(20) AUTO_INCREMENT PRIMARY KEY, --自增编号 log_name VARCHAR(50) NOT NULL, --登入名 log_pwd VARCHAR(20) NOT NULL,--登入密码 stu_id VARCHAR(20) NOT NULL,--学生学号 stu_name VARCHAR(20),--学生的姓名 gender ENUM('男','女') NOT NULL,--性别 birthday BIGINT(20),--生日 state ENUM('正常','在线') --状态 ) INSERT INTO stu_message(log_name,log_pwd,stu_id,stu_name,gender,birthday,state) VALUES('qijian','root','20180000','qijian','男','20200101','正常'); INSERT INTO stu_message(log_name,log_pwd,stu_id,stu_name,gender,birthday,state) VALUES('herry','root','20180001','qijian','男','20200101','正常'); INSERT INTO stu_message(log_name,log_pwd,stu_id,stu_name,gender,birthday,state) VALUES('tom','root','20180002','qijian','男','20200101','正常');
JDBC访问数据库思路:
1、加载驱动程序 Class.forName(“驱动类的类名”);在mysql8.0之前的驱动类的类名是‘com.mysql.jdbc.Driver’,mysql8.0之后的版本使用‘com.mysql.cj.jdbc.Driver’
2、获取Connection连接对象 DriverManager.getConnection(数据库连接字符串,登录用户名,登录密码)
Connection(String url,String user,String password)
参数说明:
-
- url:访问数据库的 URL 路径。
- user:是访问数据库的用户名。
- password:连接数据库的密码。
3、创建Statement对象(PreparedStatement)对象(内容:SQL语句)
4、调用Statement对象的方法:增删改操作,ExecuteUpdate 查ExecuteQuery.(调用Statement对象的方法,执行命令,对数据库进行增删改查等操作)
5、处理结果
6、关闭资源
注意:关闭资源时要遵循“先打开的资源后关闭,后打开的资源先关闭“
.java文件
1 package com.qijian.dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 8 public class student { 9 // 定义为静态方法有益于节约系统的资源 10 private static final String url = "jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8"; 11 private static final String username = "root"; 12 private static final String password = "root"; 13 14 public static void main(String[] args) throws Exception { 15 Connection con = null; 16 Statement stm = null; 17 // 1 加载驱动类,需要类的全名:包名.类名 18 // Class.forName("com.mysql.jdbc.Driver");//mysql8.0版本之前 19 // 提示信息表明数据库驱动com.mysql.jdbc.Driver'已经被弃用了、应当使用新的驱动com.mysql.cj.jdbc.Driver' 20 Class.forName("com.mysql.cj.jdbc.Driver");//mysql8.0之后 21 22 // 2 获取连接对象:数据库连接字符串,登录用户名,登录密码 23 con = DriverManager.getConnection(url, username, password); 24 25 // 3,创建 Statement对象,建立命令 26 String sql = "select * from stu_message"; 27 stm = con.createStatement(); 28 29 // 4,调用Statement对象的方法,执行命令,对数据库进行增删改查等操作 30 ResultSet rs = stm.executeQuery(sql); 31 32 // 5,处理结果 33 while (rs.next()) { 34 System.out.print("id:" + rs.getInt("id")); 35 System.out.print(" 登入名:" + rs.getString("log_name")); 36 System.out.print(" 密码:" + rs.getString("log_pwd")); 37 System.out.print(" 学号:" + rs.getString("stu_id")); 38 System.out.print(" 姓名:" + rs.getString("stu_name")); 39 System.out.print(" 性别:" + rs.getString("gender")); 40 System.out.println(" 生日:" + rs.getInt("birthday")); 41 System.out.println(" 状态:" + rs.getString("state")); 42 System.out.println(); 43 } 44 45 // 6关闭资源 46 if (rs != null) 47 rs.close(); 48 if (stm != null) 49 stm.close(); 50 if (con != null) 51 con.close(); 52 53 } 54 }
执行结果:
上面代码是针对数据的查找的操作,会发现增删改查的方法都会使用大片相同的代码。所以可以对代码进行一定的简化。如下
JDBCUtil.java文件:
1 package com.qijian.dao; 2 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 9 public class JDBCUtil { 10 // 定义为静态方法有益于节约系统的资源 11 private static final String url = "jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8"; 12 private static final String username = "root"; 13 private static final String password = "root"; 14 static String driverclass = "com.mysql.cj.jdbc.Driver"; 15 16 17 18 // 注册驱动与创建连接 19 public static Connection getConn() throws Exception { 20 Connection con = null; 21 // 1 加载驱动类,需要类的全名:包名.类名 22 // Class.forName("com.mysql.jdbc.Driver"); 23 // 提示信息表明数据库驱动com.mysql.jdbc.Driver'已经被弃用了、应当使用新的驱动com.mysql.cj.jdbc.Driver' 24 Class.forName(driverclass); 25 // 2 获取连接对象:数据库连接字符串,登录用户名,登录密码 26 con = DriverManager.getConnection(url, username, password); 27 return con; 28 } 29 30 // 关闭资源 关闭 Connection -》Statement -》ResultSet 31 public static void release(Connection conn, Statement st, ResultSet rs) { 32 closeRs(rs); 33 closeSt(st); 34 closeConn(conn); 35 } 36 37 // 关闭资源 关闭 Connection -》Statement 38 public static void release(Connection conn, Statement st) { 39 closeSt(st); 40 closeConn(conn); 41 } 42 43 //关闭ResultSet 44 private static void closeRs(ResultSet rs) { 45 try { 46 if (rs != null) 47 rs.close(); 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 } finally { 51 rs = null; 52 } 53 } 54 55 // 关闭Statement 56 private static void closeSt(Statement st) { 57 try { 58 if (st != null) 59 st.close(); 60 } catch (SQLException e) { 61 e.printStackTrace(); 62 } finally { 63 st = null; 64 } 65 } 66 67 //关闭连接connection 68 private static void closeConn(Connection conn) { 69 try { 70 if (conn != null) 71 conn.close(); 72 } catch (SQLException e) { 73 74 e.printStackTrace(); 75 } finally { 76 conn = null; 77 } 78 79 } 80 81 82 }
1 package com.qijian.test; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 8 import com.qijian.dao.JDBCUtil; 9 10 public class testStudent { 11 12 public static void findAll() throws Exception { 13 Connection conn = null; 14 Statement st = null; 15 conn = JDBCUtil.getConn(); 16 // 3,创建 Statement对象,建立命令 17 String sql = "select * from stu_message"; 18 st = conn.createStatement(); 19 20 // 4,调用Statement对象的方法,执行命令,对数据库进行增删改查等操作 21 ResultSet rs = st.executeQuery(sql); 22 23 // 5,处理结果 24 while (rs.next()) { 25 System.out.print("id:" + rs.getInt("id")); 26 System.out.print(" 登入名:" + rs.getString("log_name")); 27 System.out.print(" 密码:" + rs.getString("log_pwd")); 28 System.out.print(" 学号:" + rs.getString("stu_id")); 29 System.out.print(" 姓名:" + rs.getString("stu_name")); 30 System.out.print(" 性别:" + rs.getString("gender")); 31 System.out.println(" 生日:" + rs.getInt("birthday")); 32 System.out.println(" 状态:" + rs.getString("state")); 33 System.out.println(); 34 } 35 36 JDBCUtil.release(conn, st, rs); 37 38 } 39 40 public static void insert() throws Exception { 41 Connection conn = null; 42 PreparedStatement pstm = null; 43 conn = JDBCUtil.getConn(); 44 45 // 3,创建prepareStatement对象,建立命令 46 String sql = "INSERT INTO stu_message(log_name,log_pwd,stu_id,stu_name,gender,birthday,state) VALUES('lisi','root','20180002','qijian','男','20200101','正常')"; 47 pstm = conn.prepareStatement(sql); 48 49 // 4,调用prepareStatement对象的方法,执行命令,对数据库进行增删改查等操作 50 int rows = pstm.executeUpdate(); 51 52 // 5 处理结果 53 if (rows > 0) { 54 System.out.println("新增成功"); 55 } else { 56 System.out.println("新增失败"); 57 } 58 59 JDBCUtil.release(conn, pstm); 60 } 61 62 public static void updata() throws Exception { 63 Connection conn = null; 64 PreparedStatement pstm = null; 65 conn = JDBCUtil.getConn(); 66 // 3,创建prepareStatement对象,建立命令 67 String sql = "UPDATE stu_message SET log_name='tomp' WHERE id = 3;"; 68 pstm = conn.prepareStatement(sql); 69 // 4,调用prepareStatement对象的方法,执行命令,对数据库进行增删改查等操作 70 int rows = pstm.executeUpdate(); 71 // 5 处理结果 72 if (rows > 0) { 73 System.out.println("修改成功!!!"); 74 } else { 75 System.out.println("修改失败!!!"); 76 } 77 78 // 6关闭资源 79 JDBCUtil.release(conn, pstm); 80 } 81 82 public static void delete() throws Exception { 83 Connection conn = null; 84 PreparedStatement pstm = null; 85 conn = JDBCUtil.getConn(); 86 87 // 3,创建prepareStatement对象,建立命令 88 String sql = "DELETE FROM stu_message WHERE id = 5"; 89 pstm = conn.prepareStatement(sql); 90 91 // 4,调用prepareStatement对象的方法,执行命令,对数据库进行增删改查等操作 92 int rows = pstm.executeUpdate(); 93 94 // 5 处理结果 95 if (rows > 0) { 96 System.out.println("删除成功!!!"); 97 } else { 98 System.out.println("删除失败!!!"); 99 } 100 101 // 6关闭资源 102 JDBCUtil.release(conn, pstm); 103 } 104 105 public static void main(String[] args) throws Exception { 106 testStudent.findAll(); 107 // testStudent.delete(); 108 // testStudent.insert(); 109 // testStudent.updata(); 110 } 111 112 }
结果: