DBUtil
package day_8_3_1;
import java.io.IOException;
import java.util.Properties;
/**
*
* 只加载一次
* @author 啊超
* @Date 2021年8月3日
*/
public class PropertiesUtil {
private static Properties properties = null;
public static Properties getProperties() {
if (properties == null) {
properties = new Properties();
// DBUtil.class : 获取运行时类
// getClassLoader : 获取类加载器
// getResourceAsStream : 把对应的资源转换为流对象
// 直接写名字 默认定义为src下
try {
properties.load(DBUtil.class.getClassLoader()
.getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
return properties;
}
}
package day_8_3_1;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
public static Connection getConnection() throws SQLException,
ClassNotFoundException, IOException {
Connection conn = null;
Properties properties = PropertiesUtil.getProperties();
// 1 加载驱动/驱动注册
Class.forName(properties.getProperty("driver"));
// 2 链接对象 , 所有的包 全都是java.sql的
conn = DriverManager.getConnection(properties.getProperty("url"),
properties.getProperty("username"),
properties.getProperty("password"));
return conn;
}
public static void close(AutoCloseable obj) {
// 6 关闭资源
if (obj != null) {
try {
obj.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=root
Client
package day_8_3_1;
import java.util.Scanner;
/**
*
* 用户端
* @author 啊超
* @Date 2021年8月4日
*/
public class Client {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
while(true){
System.out.println("请输入 1登录 2注册 ");
int no=scanner.nextInt();
if(no==1){
//登录
System.out.println("请输入用户名");
String username =scanner.next();
System.out.println("请输入密码");
String password=scanner.next();
Seriver.login(username,password);
}else if(no==2){
System.out.println("请输入用户名");
String username=scanner.next();
System.out.println("请输入密码");
String password=scanner.next();
System.out.println("请确认密码");
String newpassword=scanner.next();
while(!password.equals(newpassword)){
System.out.println("请输入密码");
password=scanner.next();
System.out.println("请确认密码");
newpassword=scanner.next();
}
Seriver.register(username,password);
}else{
System.out.println("没有该功能");
}
}
}
}
Seriver
package day_8_3_1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import day_8_3_1.DBUtil;
public class Seriver {
public static void login(String username,String password){
//提供方法
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
//查询数据库
try{
connection=DBUtil.getConnection();
preparedStatement=connection.prepareStatement("select*from user where username=?");
preparedStatement.setString(1,username);
resultSet=preparedStatement.executeQuery();
if(resultSet.next()){
//到这里是用户名正确,该查看密码了
String oldPassword =resultSet.getString("password");
if(oldPassword.equals(password)){
System.out.println("登陆成功");
}else{
System.out.println("密码错误");
}
}else{
System.out.println("用户名不存在");
}
}catch(Exception e){
e.printStackTrace();
}finally{
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
public static void register(String username,String password){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection
.prepareStatement("select * from user where username = ?");
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
System.err.println("用户名已存在");
} else {
preparedStatement.close();
preparedStatement=connection.prepareStatement("insert into user (username,password) values (?,?)");
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
preparedStatement.executeUpdate();
System.out.println("注册成功");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
}