package com.NBUFE.dao;
import com.NBUFE.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Userdao {
public boolean register(String username, String password,int type) throws SQLException {
Connection connection = DBUtil.getconnection();
String sql = "INSERT INTO tb_user (username,password,type) VALUES (?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);
statement.setInt(3,type);
int i = statement.executeUpdate();
if (i != 0) {
return true;
} else {
return false;
}
}
public boolean login(String username, String password,int type) throws SQLException {
Connection connection = DBUtil.getconnection();
String sql = "select * from tb_user where username=? and password=? and type=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);
statement.setInt(3,type);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return true;
} else {
return false;
}
}
private void xiugai(int id,String) 4{
}
package com.NBUFE.util;
import java.sql.*;
public class DBUtil {
public static Connection getconnection() {
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection= DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=123");
System.out.println("创建连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void Close(ResultSet resultSet,Connection connection,PreparedStatement preparedStatement)
{
if(resultSet!=null)
{
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}