package constant;
import com.QQJson;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public final class QQDB {
//此方法为获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
String driver = QQCom.driver; //数据库驱动
String url = QQCom.urlAll;//数据库
String user = QQCom.username; //用户名
String password = QQCom.password; //密码
Class.forName(driver); //加载数据库驱动
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can't find the Driver!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static int exeSql(String sql) {
int result = 0;
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
} catch (SQLException err) {
err.printStackTrace();
free(null, stmt, conn);
} finally {
free(null, stmt, conn);
}
return result;
}
public static int exeSqlGetID(String sql) {
int result = 0;
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
result = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
result = rs.getInt(1);
}
} catch (SQLException err) {
err.printStackTrace();
free(null, stmt, conn);
} finally {
free(null, stmt, conn);
}
return result;
}
/**
* 批量插入
*
* @param lt
* @return
*/
public static int exeSqlTran(List<String> lt) {
int result = 0;
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
conn.setAutoCommit(false);
for (String s : lt) {
stmt.addBatch(s);
}
result = stmt.executeBatch().length;
conn.commit(); // 事务提交
conn.setAutoCommit(true);
} catch (SQLException err) {
err.printStackTrace();
free(null, stmt, conn);
} finally {
free(null, stmt, conn);
}
return result;
}
public static <T> List<T> GetList(String sql, Class<?> eClass) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<T> lt = new ArrayList<>();
try {
conn = getConnection();// 创建MySQL连接,参数依次为数据库连接地址、用户和密码
pstmt = conn.prepareStatement(sql);
// 拼接查询SQL,将id的值拼接到querySql中的第一个问号里
rs = pstmt.executeQuery();
// 获取ResultSet对象的列的数量、类型和属性。
ResultSetMetaData md = rs.getMetaData();
// 将ResultSet对象的列名和值存到map中,再将map转换为json字符串,最后将json字符串转换为实体类对象
Map<String, Object> rowData = new HashMap<>();
while (rs.next()) {
for (int i = 1; i <= md.getColumnCount(); i++) {
rowData.put(md.getColumnLabel(i), rs.getObject(i));
}
String jsonStr = QQJson.toJsonString(rowData);
T t = (T) QQJson.toModel(jsonStr, eClass);
lt.add(t);
}
} catch (SQLException err) {
err.printStackTrace();
free(rs, pstmt, conn);
}
return lt;
}
//region 不用
public static void free(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException err) {
err.printStackTrace();
}
}
public static void free(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException err) {
err.printStackTrace();
}
}
public static void free(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException err) {
err.printStackTrace();
}
}
public static void free(ResultSet rs, Statement st, Connection conn) {
free(rs);
free(st);
free(conn);
}
//endregion
}
Java数据库批量添加
最新推荐文章于 2024-07-22 16:19:47 发布