第十五周作业
题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
代码
package cn.edu.ccut.util; 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 ser=new Scanner(System.in); System.out.println("请输入用户名和密码 "); String username=ser.nextLine(); String password=ser.nextLine(); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from t_login where username = '"+username+"' and password = '"+password+"'"); if(rs.next()){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } 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表进行一条记录的添加操作。
text代码
package cn.edu.ccut.util; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class text { public static void main(String[] args) { DB db = DB.getInstance();//创建DB对象 Scanner reader = new Scanner(System.in); System.out.print("请输入用户名和密码:\n"); String username= reader.nextLine(); String password= reader.nextLine();//获取用户名和密码 ResultSet rs =null; rs = db.DBQuery("select * from t_login where username = ? and password = ?", new Object[]{username,password});//调用DBQuery进行查询 try { if(rs.next()){//next()指针遍历结果集,未拨动则未查询到 System.out.print("登录成功!t_user表内容为:\n"); rs = db.DBQuery("select * from t_user", new Object[]{});//调用DBQuery进行查询 char sex = ' '; while(rs.next()){ if(rs.getInt(3)==0){ sex = '男'; }else if(rs.getInt(3)==1){ sex = '女'; } System.out.println("name:"+rs.getString(2)+" sex:"+sex+" birthday:"+rs.getString(4));//进行显示 } System.out.print("请输入对t_user表进行一条记录的添加信息:\n"); String name; String birthday;//暂存添加信息 name=reader.nextLine(); sex=reader.nextLine().charAt(0); birthday=reader.nextLine(); int isex = -1;//初值-1 if(sex=='男'){ isex=0; }else if(sex=='女'){ isex=1; } if(db.DBModify("insert into t_user (name,sex,birthday)values(?,?,?)", new Object[]{name,isex,birthday})>0){//调用DBModify添加且判断 System.out.print("添加成功\n"); } }else{ System.out.print("登录失败\n"); } } catch (SQLException e) { e.printStackTrace(); } db.close();//调用方法关闭数据库连接 reader.close(); } }
DB代码
package cn.edu.ccut.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DB { private static DB db; private Connection con; private PreparedStatement ps; private ResultSet rs; public static DB getInstance(){//单态构造方法 if(db == null){ db = new DB(); } return db; } static{//静态代码块加载JBDC驱动程序 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private DB(){//构造方法创建数据库连接 try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",""); } catch (SQLException e) { e.printStackTrace(); } } public ResultSet DBQuery(String sql,Object[] field){//自定义预处理查询方法 try { ps = con.prepareStatement(sql); if(field.length>0){ for(int i = 0;i<field.length;i++){ ps.setObject(i+1, field[i]);//数组传值从0记,预处理传从1 } } rs = ps.executeQuery();//用ResultSet对象存放查询暂存结果 } catch (SQLException e) { e.printStackTrace(); } return rs; } public int DBModify(String sql,Object[] field){//修改方法 int n = 0; try { ps = con.prepareStatement(sql); if(field.length>0){ for(int i = 0;i<field.length;i++){ ps.setObject(i+1, field[i]);//数组传值从0记,预处理传从1 } } n = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return n; } public void close(){//关闭数据库连接方法 try { if(rs!=null){ rs.close(); } ps.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
结果