package com.login; import java.sql.*; import java.util.Scanner; public class login { 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(); String sql="select * from student where username='"+username+"'and password='"+password+"'"; try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/student"; con=DriverManager.getConnection(url, "root","0000"); 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表进行一条记录的添加操作。
package com.login; import java.sql.*; import java.util.Scanner; public class usermodify { public static void main(String[] args) { Connection con=null; Statement st=null; boolean rs; ResultSet rt=null; Scanner sc=new Scanner(System.in); System.out.println("请输入用户名"); String username=sc.next(); System.out.println("请输入密码"); String password=sc.next(); String sql="select * from login where username='"+username+"'and password='"+password+"'"; try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/dbfjd"; con=DriverManager.getConnection(url, "root","0000"); st=con.createStatement(); rs=st.execute(sql); if(username.equalsIgnoreCase(username)&&password.equalsIgnoreCase(password)){ System.out.println("登陆成功"); rt=DB.getInstance().executeSelect("select * from user", args); while(rt.next()){ System.out.println("生日"+rt.getString(2)+"性别"+rt.getString(3)+"姓名"+rt.getString(4)); } System.out.println("请输入生日"); String birthday=sc.next(); System.out.println("请输性别"); String sex=sc.next(); System.out.println("请输入姓名"); String name=sc.next(); String sql1="insert into user (birthday,sex,name)values('"+birthday+"','"+sex+"','"+name+"')"; int i=DB.getInstance().executeModify(sql1, args); if(i>0){ System.out.println("插入成功"); }else{ System.out.println("插入失败"); } }else{ System.out.println("登陆失败"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }