Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Development Lib > Code Generator &&
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
31 Star 259 Fork 131

小螺旋丸/codeMan

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (2)
master
v2.35
v2.33
master
Branches (1)
Tags (2)
master
v2.35
v2.33
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (2)
master
v2.35
v2.33
codeMan
/
src
/
main
/
java
/
util
/
DBUtils.java
codeMan
/
src
/
main
/
java
/
util
/
DBUtils.java
DBUtils.java 15.16 KB
Copy Edit Raw Blame History
zhangruixuann authored 2022年01月05日 09:19 +08:00 . fix:mysql8.0连接失败问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
package util;
import constant.CodeConstant;
import constant.Constant;
import entity.Parameters;
import entity.TableNameAndType;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class DBUtils {
public static Connection getConnection(Parameters parameters) {
Connection conn = null;
String url;
String driverClass;
if ("mysql".equals(parameters.getDataBaseTypeVal())) {
try {
url = "jdbc:mysql://" + parameters.getDataBaseIpVal() + ":" + parameters.getDataBasePortVal() + "/"
+ parameters.getDataBaseNameVal()
+ "?allowPublicKeyRetrieval=true&useSSL=false&connectTimeout=10000&socketTimeout=10000&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull";
driverClass = "com.mysql.cj.jdbc.Driver";
parameters.setDataBaseUrl(url);
parameters.setDataBaseDriverClass(driverClass);
Class.forName(driverClass);
DriverManager.setLoginTimeout(10);
conn = DriverManager.getConnection(url, parameters.getDataBaseUserNameVal(),
parameters.getDataBasePwdVal());
} catch (Exception e) {
JOptionPane.showMessageDialog(Constant.frmv, "数据库连接失败!请检查数据库类型是否选择正确,相关配置是否填写正确!mysql请使用5.7及以上版本" + CodeConstant.NEW_LINE
+ "错误信息:" + e.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
return null;
}
} else if ("oracle".equals(parameters.getDataBaseTypeVal())) {
try {
url = "jdbc:oracle:thin:@//" + parameters.getDataBaseIpVal() + ":" + parameters.getDataBasePortVal() + "/"
+ parameters.getDataBaseNameVal();
driverClass = "oracle.jdbc.OracleDriver";
parameters.setDataBaseUrl(url);
parameters.setDataBaseDriverClass(driverClass);
Class.forName(driverClass);
Properties info = new Properties();
info.put("oracle.net.CONNECT_TIMEOUT", 10000);
info.put("oracle.jdbc.ReadTimeout", 10000);
info.put("user", parameters.getDataBaseUserNameVal());
info.put("password", parameters.getDataBasePwdVal());
DriverManager.setLoginTimeout(10);
conn = DriverManager.getConnection(url, info);
} catch (Exception e) {
JOptionPane.showMessageDialog(Constant.frmv, "数据库连接失败!请检查数据库类型是否选择正确,相关配置是否填写正确!" + CodeConstant.NEW_LINE
+ "错误信息:" + e.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
return null;
}
} else if ("postgresql".equals(parameters.getDataBaseTypeVal())) {
try {
url = "jdbc:postgresql://" + parameters.getDataBaseIpVal() + ":" + parameters.getDataBasePortVal()
+ "/" + parameters.getDataBaseNameVal() + "?socketTimeout=1&connectTimeout=1&useUnicode=true&characterEncoding=UTF-8";
driverClass = "org.postgresql.Driver";
parameters.setDataBaseUrl(url);
parameters.setDataBaseDriverClass(driverClass);
Class.forName(driverClass);
DriverManager.setLoginTimeout(10);
conn = DriverManager.getConnection(url, parameters.getDataBaseUserNameVal(),
parameters.getDataBasePwdVal());
} catch (Exception e) {
JOptionPane.showMessageDialog(Constant.frmv, "数据库连接失败!请检查数据库类型是否选择正确,相关配置是否填写正确!" + CodeConstant.NEW_LINE
+ "错误信息:" + e.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
return null;
}
} else if ("sqlserver".equals(parameters.getDataBaseTypeVal())) {
try {
url = "jdbc:sqlserver://" + parameters.getDataBaseIpVal() + ":" + parameters.getDataBasePortVal() + ";DatabaseName=" + parameters.getDataBaseNameVal();
driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
parameters.setDataBaseUrl(url);
parameters.setDataBaseDriverClass(driverClass);
Class.forName(driverClass);
DriverManager.setLoginTimeout(10);
conn = DriverManager.getConnection(url, parameters.getDataBaseUserNameVal(),
parameters.getDataBasePwdVal());
} catch (Exception e) {
JOptionPane.showMessageDialog(Constant.frmv, "数据库连接失败!请检查数据库类型是否选择正确,相关配置是否填写正确!" + CodeConstant.NEW_LINE
+ "错误信息:" + e.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
return null;
}
}
return conn;
}
/**
* 获取表的主键id
*
* @param parameters
* @param tableName
* @return
* @throws Exception
*/
public static List<String> getPrimarykey(Parameters parameters, String tableName) throws Exception {
List<String> primaryKeyList = new ArrayList<>(3);
Connection connection = getConnection(parameters);
if (connection == null) {
return null;
}
DatabaseMetaData metaData = connection.getMetaData();
String userName = connection.getMetaData().getUserName();
if ("oracle".equals(parameters.getDataBaseTypeVal())) {
tableName = tableName.toUpperCase();
} else if ("postgresql".equals(parameters.getDataBaseTypeVal())) {
if (tableName.contains(".")) {
String[] split = tableName.split("\\.");
tableName = split[split.length - 1];
}
}
// 获取主键字段
ResultSet primaryKeys = metaData.getPrimaryKeys(connection.getCatalog(), "oracle".equals(parameters.getDataBaseTypeVal()) ? userName : connection.getSchema(), tableName);
//根据结果集元数据打印内容
ResultSetMetaData pkmd = primaryKeys.getMetaData();
while (primaryKeys.next()) {
for (int i = 1; i <= pkmd.getColumnCount(); i++) {
//获取主键的名称
if ("COLUMN_NAME".equalsIgnoreCase(pkmd.getColumnName(i))) {
primaryKeyList.add(primaryKeys.getString(i));
}
}
}
connection.close();
return primaryKeyList;
}
/**
* @param tableNameVal
* @param connection
* @return
*/
private static Map<String, String> getColumnComment(String databaseType, String tableNameVal, Connection connection) {
PreparedStatement columnListPst = null;
ResultSet columnListRs = null;
String sql = "";
if ("mysql".equals(databaseType)) {
sql = "show full columns from `" + tableNameVal + "`";
} else if ("postgresql".equals(databaseType)) {
String[] tableNameArr = tableNameVal.split("\\.");
if (tableNameArr.length == 1) {
tableNameVal = tableNameArr[0];
} else {
tableNameVal = tableNameArr[1];
}
sql = "SELECT" +
" A.attname AS \"Field\"," +
" col_description ( A.attrelid, A.attnum ) AS \"Comment\"" +
" FROM" +
" pg_class AS C," +
" pg_attribute AS A " +
" WHERE" +
" C.relname = '" + tableNameVal + "' " +
" AND A.attrelid = C.oid " +
" AND A.attnum > 0";
} else if ("oracle".equals(databaseType)) {
sql = "select column_name AS \"Field\",comments AS \"Comment\" from user_col_comments where lower(table_Name)='" + tableNameVal.toLowerCase() + "'";
} else if ("sqlserver".equals(databaseType)) {
sql = "SELECT\n" +
"\tcast(col.name as varchar(500)) AS \"Field\",\n" +
"\tcast(ep.[value] as varchar(500)) AS \"Comment\" \n" +
"FROM\n" +
"\tdbo.syscolumns col\n" +
"\tLEFT JOIN dbo.systypes t ON col.xtype= t.xusertype\n" +
"\tINNER JOIN dbo.sysobjects obj ON col.id= obj.id \n" +
"\tAND obj.xtype= 'U' \n" +
"\tAND obj.status >= 0\n" +
"\tLEFT JOIN dbo.syscomments comm ON col.cdefault= comm.id\n" +
"\tLEFT JOIN sys.extended_properties ep ON col.id= ep.major_id \n" +
"\tAND col.colid= ep.minor_id \n" +
"\tAND ep.name= 'MS_Description'\n" +
"\tLEFT JOIN sys.extended_properties epTwo ON obj.id= epTwo.major_id \n" +
"\tAND epTwo.minor_id= 0 \n" +
"\tAND epTwo.name= 'MS_Description' \n" +
"WHERE\n" +
"\tobj.name= '" + tableNameVal + "'";
}
// 列名集合
Map<String, String> commentMqp = new HashMap<>();
try {
columnListPst = connection.prepareStatement(sql);
columnListRs = columnListPst.executeQuery();
while (columnListRs.next()) {
commentMqp.put(columnListRs.getString("Field").toUpperCase(), columnListRs.getString("Comment"));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(Constant.frmv, "获取数据库字段注释的时候出错(不影响后续使用)" + CodeConstant.NEW_LINE
+ "错误信息:" + e.getMessage(), "提示",
JOptionPane.WARNING_MESSAGE);
return commentMqp;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {
}
try {
if (columnListPst != null) {
columnListPst.close();
}
} catch (SQLException ignored) {
}
try {
if (columnListRs != null) {
columnListRs.close();
}
} catch (SQLException ignored) {
}
}
return commentMqp;
}
public static List<String> getColumnNameList(String databaseType, String tableNameVal, Connection connection) {
PreparedStatement columnListPst = null;
ResultSet columnListRs = null;
String sql = "";
if ("mysql".equals(databaseType)) {
sql = "select * from `" + tableNameVal + "` where 1=0";
} else if ("oracle".equals(databaseType)) {
sql = "select * from " + tableNameVal + " where 1=0";
} else if ("postgresql".equals(databaseType)) {
sql = "select * from " + tableNameVal + " where 1=0";
} else if ("sqlserver".equals(databaseType)) {
sql = "select * from " + tableNameVal + " where 1=0";
}
// 列名集合
List<String> columnList = new ArrayList<>();
try {
columnListPst = connection.prepareStatement(sql);
columnListRs = columnListPst.executeQuery();
ResultSetMetaData metaData = columnListRs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 0; i < columnCount; i++) {
columnList.add(metaData.getColumnName(i + 1));
}
} catch (SQLException e) {
return null;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {
}
try {
if (columnListPst != null) {
columnListPst.close();
}
} catch (SQLException ignored) {
}
try {
if (columnListRs != null) {
columnListRs.close();
}
} catch (SQLException ignored) {
}
}
return columnList;
}
/**
* 获取表中所有字段名称和类型
*
* @return
*/
public static List<TableNameAndType> getColumnNameAndTypes(String databaseType, String tableNameVal,
Connection connection) {
PreparedStatement columnListPst = null;
ResultSet columnListRs = null;
String sql = "";
if ("mysql".equals(databaseType)) {
sql = "select * from `" + tableNameVal + "` where 1=0";
} else if ("oracle".equals(databaseType)) {
sql = "select * from " + tableNameVal + " where 1=0";
} else if ("postgresql".equals(databaseType)) {
sql = "select * from " + tableNameVal + " where 1=0";
} else if ("sqlserver".equals(databaseType)) {
sql = "select * from " + tableNameVal + " where 1=0";
}
// 列名集合
List<TableNameAndType> nameAndTypes = new ArrayList<>();
try {
columnListPst = connection.prepareStatement(sql);
columnListRs = columnListPst.executeQuery();
ResultSetMetaData metaData = columnListRs.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i = 0; i < columnCount; i++) {
TableNameAndType nameAndType = new TableNameAndType();
nameAndType.setName(metaData.getColumnName(i + 1));
//设置sqlParamName
nameAndType.setSqlParamName(DataUtils.getSqlParam(metaData.getColumnName(i + 1)));
nameAndType.setComment((metaData.getColumnName(i + 1)));
String javaTypeName = "";
String javaClassName = "";
String columnTypeName = metaData.getColumnTypeName(i + 1).toUpperCase();
switch (databaseType) {
case "postgresql":
switch (columnTypeName) {
case "INT2":
case "SERIAL4":
case "SERIAL2":
case "INT4":
javaTypeName = "Integer";
break;
case "INT8":
case "SERIAL8":
javaTypeName = "Long";
break;
case "DATE":
case "TIMESTAMPTZ":
case "TIMESTAMP":
case "TIMETZ":
case "TIME":
javaTypeName = "Date";
javaClassName = "java.util.Date";
break;
default:
javaTypeName = "String";
break;
}
break;
case "mysql":
switch (columnTypeName) {
case "INT":
case "MEDIUMINT":
case "BIT":
case "TINYINT":
case "SMALLINT":
case "BOOLEAN":
javaTypeName = "Integer";
break;
case "BLOB":
javaTypeName = "byte[]";
break;
case "INTEGER":
case "BIGINT":
// 主键
case "ID":
javaTypeName = "Long";
break;
case "FLOAT":
javaTypeName = "Float";
break;
case "DOUBLE":
javaTypeName = "Double";
break;
case "DECIMAL":
javaTypeName = "BigDecimal";
javaClassName = "java.math.BigDecimal";
break;
case "DATE":
case "DATETIME":
case "TIME":
case "TIMESTAMP":
case "YEAR":
javaTypeName = "Date";
javaClassName = "java.util.Date";
break;
default:
javaTypeName = "String";
break;
}
break;
case "oracle":
switch (columnTypeName) {
case "NUMBER":
javaTypeName = "Integer";
break;
case "DATE":
case "TIMESTAMP":
javaTypeName = "Date";
javaClassName = "java.util.Date";
break;
default:
javaTypeName = "String";
break;
}
break;
case "sqlserver":
switch (columnTypeName) {
case "BIGINT":
javaTypeName = "Long";
break;
case "DOUBLE":
javaTypeName = "Double";
break;
case "INTEGER":
javaTypeName = "Integer";
break;
case "DATE":
case "TIME":
case "TIMESTAMP":
case "DATETIME":
case "DATETIME2":
javaTypeName = "Date";
javaClassName = "java.util.Date";
break;
default:
javaTypeName = "String";
break;
}
default:
break;
}
nameAndType.setTypeName(javaTypeName);
nameAndType.setClassName(javaClassName);
nameAndTypes.add(nameAndType);
}
//System.out.println(nameAndTypes);
// 注释map
Map<String, String> columnComment = getColumnComment(databaseType, tableNameVal, connection);
for (TableNameAndType tableNameAndType : nameAndTypes) {
String name = tableNameAndType.getName().toUpperCase();
// 设置注释内容
assert columnComment != null;
tableNameAndType.setComment(
"".equals(columnComment.get(name)) || columnComment.get(name) == null ? tableNameAndType.getName() : columnComment.get(name));
}
} catch (SQLException e) {
return null;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {
}
try {
if (columnListPst != null) {
columnListPst.close();
}
} catch (SQLException ignored) {
}
try {
if (columnListRs != null) {
columnListRs.close();
}
} catch (SQLException ignored) {
}
}
return nameAndTypes;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

代码生成器源码,可一键生成controller,service,dao,实体类,单表、多表的sql语句,日志处理、事务支持等,同时可以生成dubbo和springCloud脚手架方便开发微服务项目,能在很大程度上提高开发效率,节约开发时间。代码生成完毕后即为一个前台到后台的完整项目。目前后台支持SSM/SpringBoot,数据库支持mysql/postgresql/oracle,前台样式使用BootStrap,js支持jquery/vue,导入eclipse/idea便可运行,可根据需求自由扩展!
Cancel

Releases (2)

All

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/zrxjava/codeMan.git
git@gitee.com:zrxjava/codeMan.git
zrxjava
codeMan
codeMan
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /