第15周作业
题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
DB
1 import java.sql.*; 2 3 public class DB { 4 private Connection con; 5 private PreparedStatement pre; 6 private ResultSet rs; 7 private static DB db; 8 9 static { 10 try { 11 Class.forName("com.mysql.jdbc.Driver"); 12 } catch (ClassNotFoundException e) { 13 e.printStackTrace(); 14 } 15 } 16 17 private DB(){ 18 try { 19 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); 20 } catch (SQLException e) { 21 e.printStackTrace(); 22 } 23 } 24 25 public static DB getInstance(){ 26 if(db == null){ 27 db = new DB(); 28 } 29 return db; 30 } 31 public ResultSet executeSelect(String sql,Object[] args){ 32 try { 33 pre = con.prepareStatement(sql); 34 if (args.length!=0){ 35 for (int i = 0; i < args.length; i++) { 36 pre.setObject(i+1,args[i]); 37 } 38 } 39 rs = pre.executeQuery(); 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 return rs; 44 } 45 public int executeModify (String sql, Object[] args) { 46 int n = 0; 47 try { 48 pre = con.prepareStatement(sql); 49 if(args.length !=0) { 50 for(int i = 0;i< args.length;i++) { 51 pre.setObject(i+1, args[i]); 52 } 53 } 54 n = pre.executeUpdate(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 return n; 59 } 60 public void close() { 61 try { 62 if (rs != null) { 63 rs.close(); 64 } 65 pre.close(); 66 con.close(); 67 } catch (SQLException e) { 68 e.printStackTrace(); 69 } 70 } 71 }
TestMain
1 import java.sql.*; 2 import java.util.Scanner; 3 4 public class TestMain { 5 public static void main(String[] args) { 6 Scanner reader = new Scanner(System.in); 7 System.out.println("请输入用户名和密码"); 8 String username = reader.next(); 9 String password = reader.next(); 10 Connection con = null; 11 Statement st = null; 12 ResultSet rs=null; 13 14 try { 15 Class.forName("com.mysql.jdbc.Driver"); 16 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); 17 st = con.createStatement(); 18 String sql ="insert into t_login(username,password) values('" +username+ "','" + password + "')"; 19 20 int count =st.executeUpdate(sql); 21 if(count>0) { 22 System.out.println("登陆成功"); 23 rs= DB.getInstance().executeSelect("select * from t_user", args); 24 while(rs.next()) { 25 int id = rs.getInt(1); 26 String name = rs.getString(2); 27 int sex = rs.getInt(3); 28 String birthday = rs.getString(4); 29 System.out.println("id name sex birthday"); 30 System.out.println(id + " " + name + " " + sex + " " + birthday); 31 32 } 33 System.out.println("添加信息"); 34 String id=reader.next(); 35 String name=reader.next(); 36 Integer sex = Integer.valueOf(reader.next()); 37 String birthday = reader.next(); 38 String sql1="insert into t_user(id,name,sex,birthday) values('"+id+"','"+name+"','"+sex+"','"+birthday+"')"; 39 int count2=DB.getInstance().executeModify(sql1, args); 40 if(count2>0) 41 System.out.println("数据添加成功"); 42 else 43 System.out.println("数据添加失败"); 44 } 45 else 46 System.out.println("登陆失败"); 47 48 49 } catch (ClassNotFoundException | SQLException e) { 50 e.printStackTrace(); 51 }finally { 52 try { 53 st.close(); 54 con.close(); 55 }catch (SQLException e){ 56 e.printStackTrace(); 57 } 58 } 59 } 60 }
运行结果
数据库截图