Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package net.sf.gluebooster.java.booster.basic.db;
import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
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 net.sf.gluebooster.java.booster.basic.container.ParameterizedStatement;
/**
* Access information to connect to a database
* @author cbauer
*
* @defaultParamText connectionString used to connect to the database
* @defaultParamText classname a new instance of this class will be created. Probably the driver class.
* @defaultParamText username user of the database
* @defaultParamText password password of the user
* @defaultParamText initialStatements initial sql statements
* @defaultParamText closeConnectionImmediately should the connection be closed immediately after the access
* @defaultParamText connection the created connection
*/
public class DbAccess implements Closeable {
/**
* used to connect to the database
*/
private String connectionString;
/**
* a new instance of this class will be created. Probably the driver class.
*/
private String classname;
/**
* user of the database
*/
private String username;
/**
* password
*/
private String password;
/**
* initial sql statements
*/
private List initialStatements;
/**
* should the connection be closed immediately after the access
*/
private boolean closeConnectionImmediately;
/**
* the connection to the database
*/
private Connection connection;
/**
* Create access to a MySql database.
* Use mysql-connector. Example
*
*
*
* mysql
* mysql-connector-java
* 8.0.16
*
*
*
*
* @param hostname hostname of the database
* @param port port of the database
* @param dbName name of the database
* @param readonly should it be only a readonly access?
* @return the created access
*/
public static DbAccess createMySql(String hostname, String port, String dbName, String username, String password, boolean readonly) {
// classname would be "com.mysql.cj.jdbc.Driver" but is no longer necessary
DbAccess result = new DbAccess("jdbc:mysql://" + hostname + ":" + port + "/" + dbName
+ "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", username, password);
if (readonly) {
result.initialStatements.add("SET SESSION TRANSACTION READ ONLY");
}
return result;
}
public DbAccess(String connectionString, String username, String password) {
this.connectionString = connectionString;
this.username = username;
this.password = password;
this.initialStatements = new ArrayList();
}
public DbAccess(String connectionString, String classname, String username, String password) {
this(connectionString, username, password);
this.classname = classname;
}
/**
* Creates a db connection from the current data.
*
* @return the db connection
*/
private Connection createConnection() throws Exception {
if (classname != null) {
Class.forName(classname).newInstance();
}
Connection result = DriverManager.getConnection(connectionString, username, password);
for (String statement : initialStatements) {
result.createStatement().execute(statement);
}
return result;
}
/**
* Executes multiple statements
* @param statements will be executed
* @return a list with the results of the statements
*/
public List