题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
/** 在test数据库中创建t_login表,字段包括id、username、password,输入用户名和密码,登录成功后显示t_user表中的内容,最后添加信息,添加成功输出yes,不成功输出no。 */
DB类:
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 test; 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 (test == null) { test = new DB(); } return test; } 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类:
import java.util.Scanner;
import java.util.Scanner;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String username = sc.next();
String password = sc.next();
Connection con = null;
Statement st = null;
ResultSet rt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
st = con.createStatement();
String sql = "insert into t_login(username,password) value(?,?)";
PreparedStatement pre = (PreparedStatement) con.prepareStatement(sql);
pre.setString(1, username);
pre.setString(2, password);
int count = pre.executeUpdate();
if(count > 0) {
System.out.println("yes");
} else {
System.out.println("no");
}
rt=DB.getInstance().executeSelect("select * from t_user", args);
while(rt.next()){
System.out.println("name "+rt.getString(2)+"\t"+"sex "+rt.getString(3)+"\t"+"birthday "+rt.getString(4)+"\n");
}
System.out.println("请输入名字:");
String name=sc.next();
System.out.println("请输性别(0:男,1:女):");
String sex=sc.next();
System.out.println("请输入出生日期:");
String birthday=sc.next();
String sql1="insert into t_user (birthday,sex,name)values('"+birthday+"','"+sex+"','"+name+"')";
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String username = sc.next();
String password = sc.next();
Connection con = null;
Statement st = null;
ResultSet rt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
st = con.createStatement();
String sql = "insert into t_login(username,password) value(?,?)";
PreparedStatement pre = (PreparedStatement) con.prepareStatement(sql);
pre.setString(1, username);
pre.setString(2, password);
int count = pre.executeUpdate();
if(count > 0) {
System.out.println("yes");
} else {
System.out.println("no");
}
rt=DB.getInstance().executeSelect("select * from t_user", args);
while(rt.next()){
System.out.println("name "+rt.getString(2)+"\t"+"sex "+rt.getString(3)+"\t"+"birthday "+rt.getString(4)+"\n");
}
System.out.println("请输入名字:");
String name=sc.next();
System.out.println("请输性别(0:男,1:女):");
String sex=sc.next();
System.out.println("请输入出生日期:");
String birthday=sc.next();
String sql1="insert into t_user (birthday,sex,name)values('"+birthday+"','"+sex+"','"+name+"')";
int i=DB.getInstance().executeModify(sql1, args);
if(i > 0) {
System.out.println("yes");
} else {
System.out.println("no");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
st.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
if(i > 0) {
System.out.println("yes");
} else {
System.out.println("no");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
st.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
运行截图:
t_login表:
t_user表成功添加后: