题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
TestMain类
package ccut.ch.fifteen;
import java.sql.*;
import java.util.Scanner;
public class TestMain {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
System.out.println("请输入用户名和密码:");
String username = r.next();
String password = r.next();
Connection con = null;
Statement st = null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","");
st = con.createStatement();
String sql ="insert into t_login(username,password) values('" +username+ "','" + password + "')";
int count =st.executeUpdate(sql);
if(count>0) {
System.out.println("登陆成功");
rs= DB.getInstance().executeSelect("select * from t_user", args); //调用方法,查询表中信息
while(rs.next()) { //循环输出
int id = rs.getInt(1);
String name = rs.getString(2);
int sex = rs.getInt(3);
String birthday = rs.getString(4);
System.out.println("id "+"name "+"sex "+"birthday");
System.out.println(id + " " + name + " " + sex + " " + birthday);
}
System.out.println("请输入id,name,sex,birthday:");//增加一条记录
String id=r.next();
String name=r.next();
Integer sex = Integer.valueOf(r.next());
String birthday = r.next();
String sql1="insert into t_user(id,name,sex,birthday) values('"+id+"','"+name+"','"+sex+"','"+birthday+"')";
int Count=DB.getInstance().executeModify(sql1, args);
if(Count>0)
System.out.println("添加成功");
else
System.out.println("添加失败,填写有误");
}
else
System.out.println("登陆失败");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
try {
st.close();
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
DB类
package ccut.ch.fifteen;
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"); //加载JDBC驱动程序
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private DB(){ //DB构造方法
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","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();
}
}
}
运行结果