第十五次作业
题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
package cn.edu.ccut.po8; import java.sql.*; import java.util.Scanner; public class Tast1 { public static void main(String[] args) { Connection con=null; Statement st=null; ResultSet rs=null; Scanner sc=new Scanner(System.in); System.out.println("请输入用户名"); String username=sc.next(); System.out.println("请输入密码"); String password=sc.next(); try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/db"; con=DriverManager.getConnection(url ,"root","root"); String sql="select * from t_login where username='"+username+"'and password='"+password+"'"; st=con.createStatement(); rs=st.executeQuery(sql); if(username.equalsIgnoreCase(username)&&password.equalsIgnoreCase(password)){ System.out.println("success"); }else{ System.out.println("fail"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行截图
题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
1.DB类
package cn.edu.ccut.po8; import java.sql.*; public class DB { private Connection con; private PreparedStatement pre; private ResultSet rs; private static DB db; static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private DB() { try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useSSL=false", "root","1234"); } catch (SQLException e) { e.printStackTrace(); } } public static DB getInstance() { if(db==null) db=new DB(); return db; } public ResultSet executeSelect(String sql,Object[] args){ try { pre=con.prepareStatement(sql); if(args.length!=0) { for(int i=0;i<args.length;i++) { pre.setObject(i+1, args[i]); } } rs=pre.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } public int executeModify(String sql,Object[] args) { int n=0; try { pre=con.prepareStatement(sql); if(args.length!=0) { for(int i=0;i<args.length;i++) { pre.setObject(i+1, args[i]); } } n=pre.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return n; } public void close(){ try { if(rs!=null) rs.close(); pre.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
2、测试类
package cn.edu.ccut.po8; import java.sql.*; import java.util.*; public class Tast { public static void main(String[] args) { System.out.println("请输入用户名:"); Scanner reader1=new Scanner(System.in); String name=reader1.nextLine(); System.out.println("请输入密码:"); Scanner reader2=new Scanner(System.in); String word=reader2.nextLine(); Connection con=null; Statement st=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); // 加载JDBC驱动程序 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useSSL=false", "root",""); //建立与数据库的连接 st=con.createStatement(); //创建一个执行SQL语句的对象 String sql="select * from t_login where username='"+name+"'and password='"+word+"'"; //SQL语句 rs=st.executeQuery(sql); //查询语句 if(rs.next()) { System.out.println("登录成功!"); System.out.println("显示列表信息"); rs=DB.getInstance().executeSelect("select * from t_user", args); while(rs.next()){ System.out.println("姓名:"+rs.getString(2)+" 性别:"+rs.getString(3)+" 生日:"+rs.getString(4)); } System.out.println("添加信息"); System.out.println("请输入姓名:"); Scanner reader=new Scanner(System.in); String name1=reader.nextLine(); System.out.println("请输入性别:"); String sex=reader.nextLine(); System.out.println("请输入出生日期:"); String birthday =reader.nextLine(); String newsql="insert into t_user (name,sex,birthday) values ('"+name1+"','"+sex+"','"+birthday+"')"; int n=DB.getInstance().executeModify(newsql, args); if(n>0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } else { System.out.println("登录失败"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { st.close(); //依次关闭数据库连接 con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
3、运行截图