第15周作业
一、题目一
编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
二、代码实现
1 package kubiao; 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 import java.util.Scanner; 9 10 public class TestMain { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Scanner s=new Scanner(System.in); 15 System.out.println("用户名:"); 16 String username= s.next(); 17 System.out.println("密码:"); 18 String password= s.next(); 19 try { 20 Class.forName("com.mysql.jdbc.Driver"); 21 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root"); 22 Statement st = con.createStatement(); 23 ResultSet rs = st.executeQuery("select * from t_login where username = '"+username+"' and" 24 + " password = '"+password+"'"); 25 if(rs.next()) { 26 System.out.println("欢迎!"); 27 }else { 28 System.out.println("输入错误"); 29 } 30 st.close(); 31 con.close(); 32 }catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 catch (ClassNotFoundException e) { 36 e.printStackTrace(); 37 } 38 } 39 }
三、运行结果截图
四、题目二
在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
五、代码实现
DB.java
1 /** 2 * 连接数据库依次进行加载JDBC驱动程序 3 * 创建数据库连接 4 * 创建执行SQL语句的对象 5 * 预处理操作 6 * 关闭数据库连接 7 */ 8 package xinxi; 9 10 import java.sql.Connection; 11 import java.sql.DriverManager; 12 import java.sql.PreparedStatement; 13 import java.sql.ResultSet; 14 import java.sql.SQLException; 15 16 public class DB { 17 private Connection con; 18 private PreparedStatement pre; 19 private ResultSet rs; 20 private static DB db; 21 22 public static DB getInstance() { 23 if (db == null) { 24 db = new DB(); 25 } 26 return db; 27 } 28 29 private DB() { 30 try { 31 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root"); 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 static { 38 try { 39 Class.forName("com.mysql.jdbc.Driver"); 40 } catch (ClassNotFoundException e) { 41 e.printStackTrace(); 42 } 43 } 44 45 public ResultSet executeSelect(String sql, Object[] args) { 46 try { 47 pre = con.prepareStatement(sql); 48 if (args.length > 0) { 49 for (int i = 0; i < args.length; i++) { 50 pre.setObject(i + 1, args[i]); 51 } 52 } 53 rs = pre.executeQuery(); 54 } catch (SQLException e) { 55 56 e.printStackTrace(); 57 } 58 return rs; 59 } 60 public int executeModify(String sql,Object[]args){ 61 int n=0; 62 try { 63 pre = con.prepareStatement(sql); 64 if (args.length > 0) { 65 for (int i = 0; i < args.length; i++) { 66 pre.setObject(i + 1, args[i]); 67 } 68 } 69 n=pre.executeUpdate(); 70 }catch (SQLException e){ 71 e.printStackTrace(); 72 } 73 return n; 74 } 75 public void close(){ 76 77 try { 78 if(rs!=null){ 79 rs.close();} 80 81 pre.close(); 82 con.close(); 83 84 } catch (SQLException e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } 88 } 89 90 }
User.java
1 /** 2 * 创建DB对象,在t_login中验证信息的正确性 3 * getString方法获得表内的数据 4 * insert into将信息插入表中 5 */ 6 package xinxi; 7 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.util.Scanner; 11 12 public class User { 13 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 Scanner reader =new Scanner(System.in); 17 18 System.out.println("用户名:"); 19 String name = reader.next(); 20 System.out.println("密码:"); 21 String password = reader.next(); 22 23 DB db = DB.getInstance(); 24 String sele = "select * from t_login where username=? and password=?"; 25 Object[] object = {name, password}; 26 int flag = 0; 27 28 try { 29 ResultSet rs =db.executeSelect(sele, object); 30 if(rs.next()) { 31 System.out.println("欢迎!"); 32 flag = 1; 33 }else { 34 System.out.println("输入错误"); 35 flag = 0; 36 } 37 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 42 System.out.println("表内信息:"); 43 if(flag == 1) { 44 String sele1 = "select * from t_user"; 45 46 try { 47 ResultSet rs1 = db.executeSelect(sele1 , new Object[0]); 48 while(rs1.next()) { 49 String id = rs1.getString(1); 50 String username = rs1.getString(2); 51 String sex = rs1.getString(3); 52 String birthday = rs1.getString(4); 53 System.out.println(" " + id + ", " + username + "," + sex + "," + birthday); 54 } 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } 58 } 59 System.out.println("信息添加:"); 60 String name1 = reader.next(); 61 int sex1 = reader.nextInt(); 62 String birthday1 = reader.next(); 63 String sele2 = "insert into t_user(username, sex, birthday) values(?, ?, ?)"; 64 int rs2 = db.executeModify(sele2, new Object[] {name1, sex1, birthday1}); 65 if(rs2 > 0) { 66 System.out.println("修改成功"); 67 }else { 68 System.out.println("修改失败"); 69 } 70 db.close(); 71 } 72 }
六、运行结果截图