Mysql、达梦工具类
import java.sql.*;
public class MysqlJdbcUtil {
private String url;
private String user;
private String pwd;
public MysqlJdbcUtil(String url, String user, String pwd){
this.url = url;
this.user = user;
this.pwd = pwd;
}
public Connection getConnection(){
Connection con = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con= DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return con;
}
public PreparedStatement getPreparedStatement(Connection con, String sql){
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
}catch (SQLException e){
e.printStackTrace();
}
return ps;
}
public void closeObj(ResultSet rs, PreparedStatement ps, Connection con){
try {
if (rs != null){
rs.close();
}
if (ps != null){
ps.close();
}
if (con != null){
con.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
-
Mysql查询工具
import cn.deltaphone.dragon.modular.ops.predata.model.dto.MetaDataDTO;
import cn.deltaphone.dragon.modular.ops.predata.utils.MysqlJdbcUtil;
import org.apache.commons.compress.utils.Lists;
import java.sql.*;
import java.util.List;
/**
* @ClassName MysqlQueryTool
*/
public class MysqlQueryTool2 {
private String name;
private Integer type;
private String url;
private String user;
private String pwd;
public MysqlQueryTool2(MetaDataDTO dto){
this.name = dto.getName();
this.url = dto.getJdbcUrl();
this.user = dto.getJdbcUser();
this.pwd = dto.getJdbcPwd();
this.type = dto.getSourceType();
}
/**
*@Description: 获取所有字段
*/
public List<String> getColumns(String tableName) {
List<String> list = Lists.newArrayList();
MysqlJdbcUtil mysqlJdbcUtil = new MysqlJdbcUtil(url,user,pwd);
Connection con = mysqlJdbcUtil.getConnection();
String sql = "SELECT COLUMN_NAME FROM information_schema.`COLUMNS` WHERE table_name = '"+tableName+"'";
PreparedStatement ps = mysqlJdbcUtil.getPreparedStatement(con, sql);
ResultSet rs = null;
try {
rs = ps.executeQuery();
while (rs.next()){
list.add(rs.getString("COLUMN_NAME"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
mysqlJdbcUtil.closeObj(rs,ps,con);
}
return list;
}
/**
*@Description: 获取所有表名
*/
public List<String> getTableNames(String tableSchema) {
List<String> list
