javaweb(db)
/*UserID,UserName,sex,idNumber,Phone,User;*/
package dao;
import dbutill.db;
import java.sql.*;
import java.util.ArrayList;
import bean.*;
public class UserDao {
final String URL = "jdbc:mysql://localhost:3306/text?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
final String USER = "root";
final String PASSWORD = "123";
final String DRIVER="com.mysql.cj.jdbc.Drive";
public boolean addC(User t) throws ClassNotFoundException{
String sql = "insert into t3(UserID,UserName,sex,idNumber,Phone,User,Password) values("+t.get("UserID")+","+t.get("UserName")+","+t.get("sex")+","+t.get("idNumber")+","+t.get("Phone")+","+t.get("User")+","+t.get("Password")+")";
System.out.println(sql);
Class.forName("com.mysql.cj.jdbc.Driver");
Statement stat=null;
Connection DBUtil=null;
int act=0;
try {
DBUtil = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
stat=DBUtil.createStatement();
act=stat.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}finally {
db.close(stat,DBUtil);
}
if(act>0) {
return true;
}else {
return false;
}
}
public boolean delC(String t) throws ClassNotFoundException{
String sql="delete from t3 where UserID"+" ="+t;
Class.forName("com.mysql.cj.jdbc.Driver");
Statement stat=null;
Connection DBUtil=null;
int act=0;
try {
DBUtil = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
stat=DBUtil.createStatement();
act=stat.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}finally {
db.close(stat,DBUtil);
}
if(act>0) {
return true;
}else {
return false;
}
}
public boolean gaiC(String id,String id1,String id3) throws ClassNotFoundException{
String sql="update t3 set "+id+"='"+id1+"' where UserID='"+id3+"'";
Class.forName("com.mysql.cj.jdbc.Driver");
Statement stat=null;
Connection DBUtil=null;
int act=0;
try {
DBUtil = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
stat=DBUtil.createStatement();
act=stat.executeUpdate(sql);
}catch(Exception e) {
e.printStackTrace();
}finally {
db.close(stat,DBUtil);
}
if(act>0) {
return true;
}else {
return false;
}
}
@SuppressWarnings("null")
public ArrayList<User> getAlluserinfos() throws ClassNotFoundException, SQLException{
ArrayList<User> users=new ArrayList<>();
PreparedStatement stat=null;
Connection DBUtil=null;
String sql2 = "select * from t3";
Class.forName("com.mysql.cj.jdbc.Driver");
try {
DBUtil = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e1) {
e1.printStackTrace();
}
stat=DBUtil.prepareStatement(sql2);
ResultSet rs = stat.executeQuery();
while(rs.next()) {
User user=new User();
user.set("UserID",rs.getString("UserID"));
user.set("UserName",rs.getString("UserName"));
user.set("sex",rs.getString("sex"));
user.set("idNumber",rs.getString("idNumber"));
user.set("Phone",rs.getString("Phone"));
user.set("User",rs.getString("User"));
user.set("Password",rs.getString("Password"));
users.add(user);
}
return users;
}
}
/*UserID,
UserName,
sex,
idNumber,
Phone,
User;*/