How To Connect To Xampp Server And Create New Mysql Database Using Netbeans – Java JDBC

How To Connect To Xampp Server And Create New Mysql Database Using Netbeans – Java JDBC

How To Connect To Xampp Server And Create New Mysql Database Using Netbeans – Java JDBC – Source Code

To connect to xampp server using java programming language and netbeans IDE (integrated development environment), you need to download MySQL Connector/J. Then extract the zipped connector file and add the jar file to the project libraries. Make sure you start Apache and mysql in Xampp. If the database you are trying to create already exixts, make sure you drop it. This code connects to Xampp server and creates new database. First the code checks if the database exists, If it doesn’t, then its created.

 * How To Connect To Mysql Database In Java Using Netbeans And Create New Database Jdbc
package connecttoserverandcreatedatabase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

 * @author Genuine
public class ConnectToServerAndCreateDatabase {

     * @param args the command line arguments
    public static void main(String[] args) {
        // Declaring all variables
        String jdbcDriver = "com.mysql.cj.jdbc.Driver";
        Connection conn = null;
        String username = "root";
        String password = "";
        String serverUrl = "jdbc:mysql://localhost:3306/?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
        Statement stmt = null;
        String dbName = "products_db2";
        String checkDb = "SELECT SCHEMA_NAME FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = '" + dbName + "'";
        ResultSet rs = null;
        boolean dbFound = false;

        try {
            //check jdbc driver (mysql connector / j). Make sure the connector is configured correctly (added to libraries) before checking it.
            System.out.println("Driver Loaded");
        } catch (ClassNotFoundException ex) {
            System.out.println("Driver Failed To Load");

        try {
            //connecting to xampp server (Apache Server)
            conn = DriverManager.getConnection(serverUrl, username, password);
            System.out.println("Connected To Server Successfully");
        } catch (SQLException ex) {
            System.out.println("Failed To Connect To Server Successfully");


        try {
            // Output sql query for checking if the database exists 
            System.out.println("Check if database exists query - " + checkDb);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(checkDb);
            // Output the resultset value
            System.out.println("Result Set Value " +;
            //Move the cursor one row from its current position
            if ( {
                //If the database is found in the information_schema, set the boolean value to true
                dbFound = true;

            //If the database is no found create new database
            if (!dbFound) {
                String createNewDatabase = "CREATE DATABASE IF NOT EXISTS " + dbName + "";
                //Display database creation query in the console section.
                System.out.println("Database creation query - " + createNewDatabase);
                //Execute database creation query and store the result in integer variable. Expected reults are 0 and 1.
                int createdb = stmt.executeUpdate(createNewDatabase);
                if (createdb > 0) {
                    System.out.println(dbName + " Created Successfully");
        } catch (SQLException ex) {
            System.out.println("Error " + ex.getMessage());



After you run the above Java code you get the following output.

Driver Loaded
Connected To Server Successfully
Check if database exists query - SELECT SCHEMA_NAME FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = 'products_db2'
Result Set Value false
Database creation query - CREATE DATABASE IF NOT EXISTS products_db2
products_db2 Created Successfully
BUILD SUCCESSFUL (total time: 20 seconds)
READ MORE  JDBC How To Upload Blob Image Into Mysql Database Using Java, Xampp And Netbeans IDE

Leave a Reply

Your email address will not be published. Required fields are marked *