题目:
编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
代码:
DB类:
1 package 第15周作业; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 import com.mysql.cj.xdevapi.Statement; 10 /** 11 * 封装连接数据库以及增删改查操作。 12 * 13 */ 14 public class DB { 15 private static DB db; 16 private Connection con; 17 private PreparedStatement ps; 18 private ResultSet rs; 19 static { 20 try { 21 Class.forName("com.mysql.cj.jdbc.Driver"); 22 } catch (ClassNotFoundException e) { 23 e.printStackTrace(); 24 } 25 } 26 27 public static DB getInstance() { 28 if (db == null) { 29 db = new DB(); 30 } 31 return db; 32 } 33 34 private DB() {// 通过构造方法创建数据库链接 35 try { 36 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?serverTimezone=UTC", "root", ""); 37 } catch (SQLException e) { 38 e.printStackTrace(); 39 } 40 } 41 42 public ResultSet DBQuery(String sql, Object[] field) { 43 try { 44 ps = con.prepareStatement(sql); 45 if (field.length > 0) { 46 for (int i = 0; i < field.length; i++) { 47 ps.setObject(i + 1, field[i]); 48 } 49 } 50 rs = ps.executeQuery(); 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 return rs; 55 } 56 57 public ResultSet DBQuery(String sql) { 58 try { 59 ps = con.prepareStatement(sql); 60 rs = ps.executeQuery(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 return rs; 65 } 66 67 public int DBModify(String sql, Object[] field) { 68 int n = 0; 69 try { 70 ps = con.prepareStatement(sql); 71 if (field.length > 0) 72 for (int i = 0; i < field.length; i++) { 73 ps.setObject(i + 1, field[i]); 74 } 75 n = ps.executeUpdate(); 76 } catch (SQLException e) { 77 e.printStackTrace(); 78 } 79 return n; 80 } 81 82 public void close() { 83 try { 84 if (rs != null) { 85 rs.close(); 86 } 87 ps.close(); 88 con.close(); 89 } catch (SQLException e) { 90 e.printStackTrace(); 91 } 92 } 93 }
测试类:
1 package 第15周作业; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.Scanner; 6 7 import javax.swing.plaf.synth.SynthStyle; 8 import javax.xml.crypto.Data; 9 10 /** 11 * 12 * 13 * 调用数据库进行增删改查 14 * 15 */ 16 public class Test { 17 18 public static void main(String[] args) { 19 DB db = DB.getInstance(); 20 boolean flag = false; 21 Scanner scan = new Scanner(System.in); 22 String[] user = new String[2]; 23 user[0] = scan.next(); 24 user[1] = scan.next(); 25 String sql = "SELECT * FROM t_login where username = ? AND password=?"; 26 ResultSet rs = db.DBQuery(sql, user); 27 try { 28 if (rs.next()) { 29 System.out.println("登录成功!"); 30 flag = true; 31 } 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 if (flag) { 36 sql = "select id,name,CASE sex WHEN 0 THEN '男' ELSE '女' END as sex,birthday from t_user"; 37 rs = db.DBQuery(sql); 38 try { 39 while (rs.next()) { 40 System.out.print(rs.getInt(1)); 41 System.out.print(" "); 42 System.out.print(rs.getString(2)); 43 System.out.print(" "); 44 System.out.print(rs.getString(3)); 45 System.out.print(" "); 46 System.out.println(rs.getString(4)); 47 } 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 } 51 String name = "a"; 52 int sex = 0; 53 String date = "1998-01-01"; 54 sql = "insert into t_user (name,sex,birthday)values(?,?,?)"; 55 if (db.DBModify(sql, new Object[] { name, sex, date }) > 0) { 56 System.out.println("更新成功"); 57 } else { 58 System.out.println("数据未更改"); 59 } 60 } else { 61 System.out.println("账号或密码错误"); 62 } 63 64 } 65 66 }