You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.8 KiB
138 lines
4.8 KiB
package dao;
|
|
|
|
import java.sql.*;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import error.GExcptSQL;
|
|
import gdms.Configuration;
|
|
import org.apache.tomcat.jdbc.pool.DataSource;
|
|
import org.apache.tomcat.jdbc.pool.PoolProperties;
|
|
|
|
|
|
public class DBManagement {
|
|
|
|
public static String[] userTables = {
|
|
"administrator",
|
|
"student",
|
|
"teacher"
|
|
};
|
|
public static String[] graduationDesignTables = {
|
|
"graduation_design",
|
|
"graduation_design_finished_product",
|
|
"graduation_design_finished_product_mentor_score",
|
|
"graduation_design_finished_product_reviewer_score",
|
|
"graduation_design_information",
|
|
"graduation_design_opening_report",
|
|
"graduation_design_opening_report_opinion_record",
|
|
"graduation_design_reply",
|
|
"graduation_design_reply_opinion_record_score"
|
|
};
|
|
//<table.field,permission>
|
|
public static Map<String,String> graduationDesignStudentTables = new HashMap<>(){
|
|
{
|
|
put("graduation_design.student_id","student");
|
|
put("opening_report_secretary_team.leader_student_id","opening_report_secretary_leader");
|
|
put("opening_report_secretary_team.student1_id","opening_report_secretary_member");
|
|
put("opening_report_secretary_team.student2_id","opening_report_secretary_member");
|
|
put("reply_secretary_team.leader_student_id","reply_secretary_leader");
|
|
put("reply_secretary_team.student1_id","reply_secretary_member");
|
|
put("reply_secretary_team.student2_id","reply_secretary_member");
|
|
}
|
|
};
|
|
public static String driverClassName="org.mariadb.jdbc.Driver";
|
|
|
|
|
|
|
|
public static DataSource dataSource = new DataSource();
|
|
|
|
private static boolean ifInit = false;
|
|
|
|
public static void init(){
|
|
PoolProperties poolProperties = new PoolProperties();
|
|
poolProperties.setUrl(Configuration.dbUrl);
|
|
poolProperties.setDriverClassName(DBManagement.driverClassName);
|
|
poolProperties.setUsername(Configuration.dbUsername);
|
|
poolProperties.setPassword(Configuration.dbPassword);
|
|
dataSource.setPoolProperties(poolProperties);
|
|
try {
|
|
Class.forName(driverClassName);
|
|
} catch (ClassNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
ifInit = true;
|
|
}
|
|
public static Connection getConnection(){
|
|
if(!ifInit) return null;
|
|
try {
|
|
return DriverManager.getConnection(Configuration.dbUrl,Configuration.dbUsername,Configuration.dbPassword);
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static void delete(String table, Map<String,String> limits) throws GExcptSQL {
|
|
Delete delete = new Delete(table, limits);
|
|
String sql = delete.getSQL();
|
|
update(sql);
|
|
}
|
|
public static void insert(String table, Map<String, String> vMap) throws GExcptSQL {
|
|
Insert insert = new Insert(table, vMap);
|
|
String sql = insert.getSQL();
|
|
update(sql);
|
|
}
|
|
public static ResultSet select(List<String> columns, String table, Map<String,String> limits, int startRow, int endRow) throws Exception {
|
|
Select select = new Select(columns,table,limits,startRow,endRow);
|
|
String sql = select.getSQL();
|
|
return query(sql);
|
|
}
|
|
public static void update(String table, Map<String,String> vMap, Map<String,String> limits) throws GExcptSQL {
|
|
Update update = new Update(table,vMap,limits);
|
|
String sql = update.getSQL();
|
|
update(sql);
|
|
}
|
|
public static ResultSet query(String sql) throws Exception {
|
|
if(!ifInit) return null;
|
|
ResultSet rs = null;
|
|
Connection con = null;
|
|
try{
|
|
con = DBManagement.getConnection();
|
|
Statement stmt = con.createStatement();
|
|
rs = stmt.executeQuery(sql);
|
|
System.out.println(rs);
|
|
} catch (SQLException e) {
|
|
throw new GExcptSQL("Connect Failure");
|
|
}finally {
|
|
if(con!=null) {
|
|
try {
|
|
con.close();
|
|
} catch (SQLException e) {
|
|
throw new GExcptSQL("Connection Close Failure");
|
|
}
|
|
}
|
|
}
|
|
return rs;
|
|
}
|
|
public static void update(String sql) throws GExcptSQL {
|
|
if(!ifInit) return;
|
|
Connection con = null;
|
|
try{
|
|
con = DBManagement.getConnection();
|
|
Statement stmt = con.createStatement();
|
|
stmt.executeUpdate(sql);
|
|
} catch (SQLException e) {
|
|
throw new GExcptSQL("Connect Failure");
|
|
}finally {
|
|
if(con!=null) {
|
|
try {
|
|
con.close();
|
|
} catch (SQLException e) {
|
|
throw new GExcptSQL("Connection Close Failure");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|