第15周作业
题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
package factorial;
import java.util.*;
import java.sql.*;
public class ConnectMySql {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入名字");
String name = input.next();
System.out.println("请输入密码");
String password = input.next();
Connection con = null;
PreparedStatement pre = null;
ResultSet rs = null;
//1.
try {
Class.forName("com.mysql.jdbc.Driver");
//2.
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/t_test?useSSL=false", "root", "lijimin");
//3.
pre = con.prepareStatement(""
+ "select * from t_login where name = ? and password = ?");
pre.setString(1, name);
pre.setString(2, password);
//4.
rs = pre.executeQuery();
if(rs.next()) {
System.out.println("success");
}
else {
System.out.println("failure");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs != null) {
rs.close();
}
pre.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:

题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
Ver1.0:
db.java:
/**
* 数据库管理
*/
package factorial;
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;
public static DB getInstance() {
if(db ==null) {
db = new DB();
}
return db;
}
//无参构造方法
DB(){
try {
//连接数据库
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/t_test?useSSL=false","root", "lijimin");
} catch (SQLException e) {
e.printStackTrace();
}
}
//静态区,加载驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
*
* @param sql: 传入的sql语句
* @param args 使用预处理set****()语句放入值
* @return
*/
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 executeUpdate(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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
User.java:
package factorial;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class User {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
//登录
System.out.println("请输入名字");
String name = input.next();
System.out.println("请输入密码");
String password = input.next();
DB test = new DB();
DB test2 = new DB();
Object[] object = {name,password};int judge = 0;
String Select1 = "select * from t_login where name = ? and password = ?";
try {
ResultSet result1 =test.executeSelect(Select1,object);
if(result1.next()) {
System.out.println("登录成功");
judge = 1;
}
else {
System.out.println("登录失败");
judge = 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
//获取信息
System.out.println("信息如下");
if(judge == 1) {
String Select2 = "select * from t_user";
Object[] object2 = new Object[0];
try {
ResultSet result2 =test2.executeSelect(Select2,object2);
while(result2.next()) {
String id = result2.getString(1);
String username = result2.getString(2);
String sex = result2.getString(3);
String birthday = result2.getString(4);
System.out.println(id + "--" + username + "--" + sex + "--" + birthday);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("添加记录:");
String newName = input.next();
String newSex = input.next();
String newBirthday = input.next();
Object[] object3 = {newName,newSex,newBirthday};
String Select3 = "insert into t_user (Name,Sex,Birthday) values(?,?,?)";
int result3 =test2.executeUpdate(Select3, object3);
if(result3 > 0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
test.close();
test2.close();
}
}
结果:



浙公网安备 33010602011771号