MySQL JDBC Connector

MySQL JDBC Connector

Download JConnector from Oracle, unpack and copy to the jar file to …jre\lib\ext folder of your Java installation. It will be automatically included in the default library available to every project you create.

 

Testing the JDBC

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package helloworld;

/* JDBC-MySQL */
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

/**
*
* @author rladam
*/
public class HelloWorld {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println(“Hello World!”);

String databaseURL = “jdbc:mysql://db01prjntu:3306/atm_data”;
String user = “userid”;
String password = “password”;
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(databaseURL, user, password);
if (conn != null) {
System.out.println(“Connected to the database”);
}
} catch (ClassNotFoundException ex) {
System.out.println(“Could not find database driver class”);
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println(“An error occurred. Maybe user/password is invalid”);
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

System.out.println(“——– MySQL JDBC Connection Testing ————“);

try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException e) {
System.out.println(“Where is your MySQL JDBC Driver?”);
e.printStackTrace();
return;
}

System.out.println(“MySQL JDBC Driver Registered!”);
Connection connection = null;

try {
connection = DriverManager.getConnection(“jdbc:mysql://db01prjntu:3306/atm_data”,”root”, “Nov-f5id”);

} catch (SQLException e) {
System.out.println(“Connection Failed! Check output console”);
e.printStackTrace();
return;
}

if (connection != null) {
System.out.println(“You made it, take control your database now!”);
} else {
System.out.println(“Failed to make connection!”);
}
}

}

Comments are closed.