1import java.sql.*;
2
3class MySQLConnection{
4
5public static final String DBNAME = "name_of_the_database"; // Like "testDB"
6
7/** JDBC driver name and database URL */
8public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
9
10/** Gives access to all Databases */
11static final String ALL_DBS_URL = "jdbc:mysql://localhost/?allowPublicKeyRetrieval=true&autoReconnect=true&useSSL=false";
12
13/** Gives access to a given DB */
14static final String GIVEN_DB_URL = "jdbc:mysql://localhost/"+DBNAME+"?allowPublicKeyRetrieval=true&autoReconnect=true&useSSL=false";
15
16// Below are the USERNAME and PASSWORD used in mysql
17public static final String USER = "myUserName";
18public static final String PASS = "myGoodPassWord";
19
20/** A variable for connecting to MySQL Server */
21public static Connection conn = null;
22
23/** A variable for Preparing Statements */
24public static PreparedStatement ps = null;
25
26public static void main(String[] args)
27 throws ClassNotFoundException, SQLException {
28 // Register JDBC driver
29 Class.forName(JDBC_DRIVER);
30
31 // Open a connection with MySQL server
32 System.out.println("Connecting to all Databases path...");
33 conn = DriverManager.getConnection(ALL_DBS_URL, USER, PASS);
34 System.out.println("Connected to all Databases!");
35
36 // Test if there is the db in MySQL Server
37 // if not then create one
38 System.out.println("Testing if "+DBNAME+" exists...");
39 ps = conn.prepareStatement("CREATE DATABASE IF NOT EXISTS "+DBNAME+";");
40 ps.executeUpdate();
41 // Now db exists for sure!
42
43 // Open a connection with given DB
44 System.out.println("Connecting to "+DBNAME+" path...");
45 conn = DriverManager.getConnection(GIVEN_DB_URL, USER, PASS);
46 System.out.println("Connected to "+DBNAME+"!");
47
48 // then you can do all sort of queries that you want to do
49}
50}
1public void commit() throws SQLException {
2 getConn().commit();
3 if (logger.isTraceEnabled()) {
4 logger.trace("Batch executor commit " + idx.get() + " rows");
5 }
6 idx.set(0);
7}
8