题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
package aa; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Test { /** * 编写一个应用程序,输入用户名和密码,访问test数据库中t_login表 * (字段包括id、username、password),验证登录是否成功 */ public static void main(String[] args) { Scanner input =new Scanner(System.in); String username=input.nextLine(); String password=input.nextLine(); Connection con=null; PreparedStatement pre=null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aa", "root","1234" ); } catch (SQLException e) { e.printStackTrace(); } try { pre=con.prepareStatement("select*from bb where username=? and password=?"); pre.setString(1,username); pre.setString(2,password); ResultSet res=pre.executeQuery(); if(res.next()){ System.out.println("YES"); }else{ System.out.println("no"); } if(res!=null){ res.close(); } } catch (SQLException e) { e.printStackTrace(); } try { pre.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
package aa; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Test1 { /** 在上一题基础上,当登录成功后, 将t_user表(id、name、sex、birthday)的信息进行显示 (要求使用DB.java完成登录和获取t_user表中数据的操作), 最后再对t_user表进行一条记录的添加操作。 */ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入姓名和密码"); String username=input.nextLine(); String password=input.nextLine(); Connection con=null; PreparedStatement pre=null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aa","root","1234"); pre=con.prepareStatement("select*from bb where username=? and password=?"); pre.setString(1, username); pre.setString(2, password); ResultSet res=pre.executeQuery(); if(res.next()){ System.out.print("登陆成功! " + "t_user表的内容是 "); pre = con.prepareStatement("select*from user"); res = pre.executeQuery(); while (res.next()) { String name = res.getString(2); String sex = res.getString(3); String birthday = res.getString(4); System.out.println("name:" + res.getString("name")); System.out.println("sex:" + res.getString("sex")); System.out.println("birthday:" + res.getString("birthday")); } } System.out.println("请输入name,sex,birthday "); String name =input.nextLine(); String sex = input.nextLine(); String birthday = input.nextLine(); pre = con.prepareStatement("insert into user(name,sex,birthday) values(?,?,?)"); pre.setString(1, name); pre.setString(2, sex); pre.setString(3, birthday); int count=pre.executeUpdate(); if(count>0){ System.out.println("数据更新成功"); } else{ System.out.println("数据更新失败"); }if(res!=null){ res.close(); } } catch (SQLException e) { e.printStackTrace(); } finally{ try { pre.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }