第15周作业
题目1:
编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
代码:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class test { public static void main(String[] args) { Scanner s=new Scanner(System.in); String username= s.next(); String password= s.next(); try { Class.forName("com.mysql.jdbc.Driver"); Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","0000"); Statement st=con.createStatement(); ResultSet rs = st.executeQuery("select * from t_login where username = '"+username+"' and password = '"+password+"'"); if(rs.next()){ System.out.print("登录成功\n"); }else{ System.out.print("登录失败\n"); } if(rs!=null){ rs.close(); } st.close(); con.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } }
结果
题目2:
在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
代码:
import java.sql.*; import java.util.Scanner; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DB db=DB.getInstance(); Scanner sc=new Scanner(System.in); System.out.println("请输入用户名 密码:"); String username=sc.next(); String password=sc.next(); ResultSet rs =null; rs = db.excuteSelect("select * from t_login where username = ? and password = ?", new Object[]{username,password}); try { if(rs.next()){ System.out.println("登录成功"); System.out.println("查询结果为:"); rs = db.excuteSelect("select * from t_user", new Object[]{}); char sex = ' '; while(rs.next()){ if(rs.getInt(3)==0){ sex = '男'; }else if(rs.getInt(3)==1){ sex = '女'; } System.out.println("id: "+rs.getInt(1)+" name: "+rs.getString(2)+" sex: "+sex+" birthday: "+rs.getString(4)); } System.out.println("请输入 姓名 性别 生日"); String name; String birthday; name=sc.next(); sex=sc.next().charAt(0); birthday=sc.next(); int isex = 0; if(sex=='男'){ isex=0; }else if(sex=='女'){ isex=1; } if(db.executeModify("insert into t_user (name,sex,birthday)values(?,?,?)", new Object[]{name,isex,birthday})>0){ System.out.println("添加成功"); } }else{ System.out.println("登录失败"); } } catch (SQLException e) { e.printStackTrace(); } db.close(); sc.close(); } }
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) { // TODO Auto-generated catch block e.printStackTrace(); } } private DB(){ try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","0000"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static DB getInstance(){ if(db==null){ db=new DB(); } return db; } public ResultSet excuteSelect (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(); } } }
运行结果