java sql connection example

Solutions on MaxInterview for java sql connection example by the best coders in the world

showing results for - "java sql connection example"
Lina
18 Sep 2017
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}
Lison
27 Jul 2016
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
queries leading to this page
java sql connection codejava connectivity with sql serverjava mssql connection examplehow to connect java and sqljava database connection exampleconnect sqlserver to javajava jdbc sql server connection example with driver classwhat is sql connection con javajdbc connection to sql databaseconnect to sql database javahow to connect java application with sql 3fjava db connectionconnect database sql server in javajdbc database connection in java syntaxjdbc sql connection examplejava jdbc sql server connection examplejava connection with sql serverjdbc connect to sql server examplehow to connect with sql using javajava sql connection querysql jdbc connection in javajdbc connection to sql serverhow to connect java to sql serverjava and sql connectionhow to connect sql server using jdbc in javahow to connect sql to javaconnect to sql server using javajdbc sql server connection javajava sql connectionjdbc database connectionjava jdbc sql connectionwhich is used to get a connection object java sqljdbc connection to sql server databasejava sql oracle connection exampleconnect java ms sqljava sql connection docssql and java connectiondatabase connection in javams sql java connectionjava connectivity with sqldatabase connection on javajava sql connectionconnect sql database jdbc using javajdbc connection to sql server in java examplejava to sql server connectionjdbc connection sqljava sql connection examplejava sql connection classconnect sql server with javajava sql server connectionhow to connect sql server database using javajava connect with sqlsql server connection with javahow to connect java with sqljdbc connection in java sql serverjava connect sql serverjava connect to sql databasejdbc connection sql server in javahow to connect sql server in javamy sql java connection stepsconnect sql to javasql server connection class in javajava ms sql server connection examplehow to connect java and sql serverjdbc connection in java for my sqljdbc connect to sql serverhow to create an sql connection in javajava database connectionhow to connect to a sql database in javasql java connectionjava connect to sql serverjava sql server connection examplems sql java connection javaconnect sql in javaconnect server to sql javaconnect sql server from javajava sql connection documentationsql connect with javahow to connect sql database in javajava and database connectionsql server connection in javahow to connect sql server database in javaconnect sql server to javawhat is a java sql connectionjava connect to sql dbhow to use java sql connectiondatabase connection javahow to connect to sql server using javajava sql connection docssql server with java connectoinconnect to sql database in javahow to use sql connector javaconnection java sqljava connect database sql serverjava connection with databasesql server connection in java servketsql connection javajava database sql connectionsql connection java examplejava sql connection example