第15周作业
题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
源程序:
package bb; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.*; public class text { public static void main(String[] args) { Scanner reader=new Scanner(System.in); System.out.println("请输入名字:"); String username=reader.next(); System.out.println("请输入密码:"); String password=reader.next(); Connection con=null; Statement st=null; PreparedStatement p=null; try { Class.forName("com.mysql.jdbc.Driver"); try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/t_login1","root","1234"); p=con.prepareStatement("select*from ss where username=? and password=?"); p.setString(1, username); p.setString(2,password); ResultSet r=p.executeQuery(); if(r.next()){ System.out.println("登录成功!"); } else{ System.out.println("登录失败!"); } if(r!=null){ r.close(); } } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally{ try { p.close(); } catch (SQLException e1) { e1.printStackTrace(); } try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
运行结果
题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
源程序
public class DB { private Connection con; private PreparedStatement pre; private ResultSet rs; private static DB db; public static DB getInstance() { if (db == null) { db = new DB(); } return db; } private DB() { try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""); } catch (SQLException e) { e.printStackTrace(); } } static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 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) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class user { public static void main(String[] args) { DB db=DB.getInstance(); Scanner reader = new Scanner(System.in); System.out.print("请输入用户名和密码:\n"); String username = reader.nextLine(); String password = reader.nextLine(); ResultSet rs = db.executeselect("select * from t1 where username=? and password=?", new Object[] {username,password}); try { if (rs.next()) { System.out.print("登陆成功!\n" + "t3表的内容是\n"); ResultSet rs1 = db.executeselect("select*from t3",new Object[]{}); while (rs1.next()) { String name = rs1.getString(2); String sex = rs1.getString(3); String birthday = rs1.getString(4); System.out.println("name:" + name); System.out.println("sex:" + sex); System.out.println("birthday:" + birthday); } System.out.print("请输入您的name,sex,birthday\n"); String name = reader.nextLine(); String sex = reader.nextLine(); String birthday = reader.nextLine(); int count=db.executeModify("insert into t3(name,sex,birthday)values(?,?,?)", new Object[]{name,sex,birthday}); if (count > 0) { System.out.print("数据更新成功"); } else { System.out.print("数据更新失败"); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } db.close(); } }
运行结果