📄 userdao.java
字号:
package students;
import java.sql.*;
public class UserDAO {
static User user = new User();
public static ResultSet queryUserByUsername(String username){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = Database.connectToDb();
String strSQL = "select * from students where ID = '"+username+"'";
stmt = con.createStatement();
rs = stmt.executeQuery(strSQL);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public static void AddUser(String stuid,String password, String name,
String sex, String enrollage, String enrollyear, String class1) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = Database.connectToDb();
String strSQL = "insert into students(ID,password,name,sex,enrollage,enrollyear,Class)"+"values('"+stuid+"','"+password+"','"+name+"','"+sex+"','"+enrollage+"','"+enrollyear+"','"+class1+"')";
stmt = con.createStatement();
stmt.executeUpdate(strSQL);
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
}
public static void Delete(String stuid){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = Database.connectToDb();
String strSQL="delete from students where ID='"+stuid+"'";
stmt = con.createStatement();
stmt.executeUpdate(strSQL);
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
}
public static void AddCrouse(String cro_id,String cro_name,String cro_teacher,String cro_value,String cro_grade,String cro_dropyear){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = Database.connectToDb();
String strSQL = "insert into crouses(cro_id, cro_name, cro_teacher, cro_value, cro_grade, cro_dropyear)"+"values('"+cro_id+"','"+cro_name+"','"+cro_teacher+"','"+cro_value+"','"+cro_grade+"','"+cro_dropyear+"')";
stmt = con.createStatement();
stmt.executeUpdate(strSQL);
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
}
public static void DeleteCrouse(String cro_id){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = Database.connectToDb();
String strSQL="delete from crouses where cro_id='"+cro_id+"'";
stmt = con.createStatement();
stmt.executeUpdate(strSQL);
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
}
public static ResultSet select(String sql){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = Database.connectToDb();
String strSQL = sql;
stmt = con.createStatement();
rs = stmt.executeQuery(strSQL);
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public static void update(String sql){
Connection con = null;
Statement stmt = null;
try {
con = Database.connectToDb();
String strSQL = sql;
stmt = con.createStatement();
stmt.executeUpdate(strSQL);
}catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -