java sql connection example

import java.sql.*;

class MySQLConnection{

public static final String DBNAME = "name_of_the_database";  // Like "testDB"

/** JDBC driver name and database URL */
public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

/** Gives access to all Databases */
static final String ALL_DBS_URL = "jdbc:mysql://localhost/?allowPublicKeyRetrieval=true&autoReconnect=true&useSSL=false";

/** Gives access to a given DB */
static final String GIVEN_DB_URL = "jdbc:mysql://localhost/"+DBNAME+"?allowPublicKeyRetrieval=true&autoReconnect=true&useSSL=false";

// Below are the USERNAME and PASSWORD used in mysql
public static final String USER = "myUserName";
public static final String PASS = "myGoodPassWord";

/** A variable for connecting to MySQL Server */
public static Connection conn = null;

/** A variable for Preparing Statements */
public static PreparedStatement ps = null;

public static void main(String[] args) 
			throws ClassNotFoundException, SQLException {
		// Register JDBC driver
		Class.forName(JDBC_DRIVER);
		
		// Open a connection with MySQL server
		System.out.println("Connecting to all Databases path...");
		conn = DriverManager.getConnection(ALL_DBS_URL, USER, PASS);
		System.out.println("Connected to all Databases!");
		
		// Test if there is the db in MySQL Server
		// if not then create one
		System.out.println("Testing if "+DBNAME+" exists...");
		ps = conn.prepareStatement("CREATE DATABASE IF NOT EXISTS "+DBNAME+";");
		ps.executeUpdate();
		// Now db exists for sure!
		
		// Open a connection with given DB
		System.out.println("Connecting to "+DBNAME+" path...");
		conn = DriverManager.getConnection(GIVEN_DB_URL, USER, PASS);
		System.out.println("Connected to "+DBNAME+"!");
  		
  		// then you can do all sort of queries that you want to do
}
}

0
0
Ohood.94 85 points

                                    public void commit() throws SQLException {
  getConn().commit();
  if (logger.isTraceEnabled()) {
    logger.trace("Batch executor commit " + idx.get() + " rows");
  }
  idx.set(0);
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
java db connection my sql java connection steps jdbc connect to sql server connection java sql JDBC connect to SQL Server example how to connect sql database in java sql and java connection connect sql to java what is a java.sql.Connection sql connection java how to connect sql server in java java ms sql server connection example sql server connection class in java database connection java how to use sql connector java jdbc connection in java sql server how to connect sql to java jdbc database connection in java syntax jdbc database connection how to connect java with sql java database connection connect server to sql java how to use java.sql.connection jdbc connection sql jdbc connection to sql server database how to connect with sql using java connect sql server to java database connection in java how to connect to a sql database in java java connection with database java connect sql server java.sql.connection query java jdbc sql server connection example with driver class java jdbc sql server connection example connect sql server from java java connect with SQL connect database sql server in java java and sql connection jdbc sql server connection java sql server connection with java jdbc connection to sql database how to connect java and sql server how to connect java and sql java sql server connection example connect to sql database in java java connect to sql db java sql oracle connection example connect sql in java jdbc connection in java for my sql java database connection example jdbc connection to sql server in java example connect to sql database java sql connect with java java mssql connection example connect sqlserver to java java sql connection java.sql.connection sql java connection java sql server connection java database sql connection sql connection java example connect sql database jdbc using java sql jdbc connection in java java connect database sql server what is sql connection con java java connect to sql server connect to sql server using java java sql connection example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source