Topic 1: write an application, a user name and password to access the test database t_login table (field, including id, username, password), to verify whether the login was successful.
Problem 2: On the basis of a title, if the login is successful, the table t_user information (id, name, sex, birthday) for display (DB.java required to complete the sign and data acquisition operations t_user table), and finally t_user table again add a record operation.
Code
DB.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; 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) { e.printStackTrace (); } } private DB(){ try { con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""); } catch (SQLException e) { e.printStackTrace (); } } public static DB getInstance(){ if(db==null){ db=new DB(); } return db; } 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) { e.printStackTrace (); } } }
Test.java
import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; public class Test { public static void main(String[] args) { Scanner r =new Scanner(System.in); System.out.println ( "Please enter your user name and password" ); String username=r.next(); String password=r.next(); Object [] a={username,password}; String sql = "select * from t_login where username=? and password=?"; DB d=DB.getInstance(); ResultSet rs=d.executeSelect(sql,a); try { if(rs.next()){ System.out.println ( "successful login \ r" ); String user="select * from t_user"; System.out.println ( "Display t_user Table" ); ResultSet usertable=d.executeSelect(user,args); System.out.println("id\t"+"name\t"+"sex\t"+"birthday\t"); while(usertable.next()){ int id=usertable.getInt(1); String name=usertable.getString(2); String sex="女"; int temp=usertable.getInt(3); if(temp==1){ sex="男"; } String birthday=usertable.getString(4); System.out.println(id+"\t"+name+"\t"+sex+"\t"+birthday+"\t"); } System.out.println ( "add a new record:" ); // int the above mentioned id = r.nextInt (); String name = r.next (); int Sex = r.nextInt (); // 0 represents the female, 1 on behalf of male String Birthday = r.next (); String insert="insert into t_user(name,sex,birthday) values(?,?,?)"; Object[] in={name,sex,birthday}; int n=d.executeModify(insert, in); if(n>0){ System.out.println ( "Add success" ); } the else System.out.println ( "add failed" ); } the else System.out.println ( "Login failed" ); } catch (SQLException e) { e.printStackTrace (); } finally{ d.close(); } } }
operation result