📄 userdao.java
字号:
package com.blue.manage.DAO;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.bluedot.usermanage.bean.User;
public class UserDAO {
private static Connection connection;
private static Properties properties ;
public static void link(){
properties = new Properties();
try{
properties.load(Thread.currentThread().getContextClassLoader().
getResourceAsStream("aaa.properties"));
}catch(IOException e){
e.printStackTrace();
}
String url = properties.getProperty("url");
String driver = properties.getProperty("drv");
String username = properties.getProperty("name");
String password = properties.getProperty("password");
try{
Class.forName(url);
connection = DriverManager.getConnection(
driver, username,password);
}catch(Exception e){
e.printStackTrace();
}
}
public static void add(User u){
link();
try{
int i=1;
String sql = "insert into users(user_name,password,name,sex,email,birthday) values(?,?,?,?,?,?)";
PreparedStatement preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(i++,u.getUsername());
preparedStatement.setString(i++,u.getPassword());
preparedStatement.setString(i++,u.getName());
preparedStatement.setString(i++,u.getSex());
preparedStatement.setString(i++,u.getEmail());
preparedStatement.setDate(i++, u.getBirthday());
preparedStatement.execute();
preparedStatement.close();
}catch(SQLException e){
throw new RuntimeException(e);
}finally{
if(connection!=null){
try {
connection.close();
} catch (SQLException e1) {
}
}
}
}
public static boolean logon(String name,String pw){
link();
boolean tag = false;
try{
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from users where user_name='"+name+"' and password ='"+pw+"'");
//select * from AUTHENTICATIONS where USER_NAME='marshal' or 'x'='x' and PASSWORD='dddd';
if(resultSet.next()){
tag = true;
}
}catch(SQLException e){
throw new RuntimeException(e);
}finally{
if(connection!=null){
try {
connection.close();
} catch (SQLException e1) {
}
}
}
return tag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -