Now I will show you how to connect to MySQL and PostgreSQL database using JDBC connector.
We need connector.
Download MySQL connector.
Download PostgreSQL connector.
In Eclipse:
Right click on your Java project -> Buil Path -> Configure Build Path -> Java Build Path and add your jar
1. Short example (for MySQL):
Connection con = null;
Statement stmt;
ResultSet rs;
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
con = DriverManager.getConnection( "jdbc:mysql://hostname:port/dbname","username", "password" );
stmt = con.createStatement();
con.close();
stmt.close();
rs.close();
2. Full example:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBexample
{
String dbLogin, dbPswd, dbUrl;
Connection con = null;
Statement stmt;
ResultSet rs;
/* There are two database systems:
* - mysql
* - pg */
public DBexample( String databaseSystem )
{
if( databaseSystem.equals( "mysql" ) )
{
this.dbLogin = "root";
this.dbPswd = "password";
this.dbUrl = "jdbc:mysql://localhost:3306/dbname";
try
{
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
con = DriverManager.getConnection( this.dbUrl, this.dbLogin, this.dbPswd );
stmt = con.createStatement();
}
catch (Exception e)
{
System.out.println( "There is no connector available." );
return;
}
System.out.println( "Connector registered" );
}
else if( databaseSystem.equals( "pg" ) )
{
this.dbLogin = "root";
this.dbPswd = "password";
this.dbUrl = "jdbc:postgresql://localhost:5432/dbname";
try
{
Class.forName( "org.postgresql.Driver" );
con = DriverManager.getConnection( this.dbUrl, this.dbLogin, this.dbPswd );
stmt = con.createStatement();
}
catch (Exception e)
{
System.out.println( "There is no connector available." );
return;
}
System.out.println( "Connector registered" );
}
}
/* Execute sql: update, insert, delete... */
public void executeUpdate(String aQuery) throws SQLException
{
stmt.executeUpdate(aQuery);
}
/* Execute sql: select... and get result */
public ResultSet executeSelect( String aQuery )
{
try
{
rs = stmt.executeQuery(aQuery);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public void closeConnection() throws SQLException
{
con.close();
stmt.close();
rs.close();
}
/* Test */
public static void main(String[] argv) throws SQLException
{
ResultSet rs;
/* Connect to MySQL and get all cars */
DBexample dbMySQL = new DBexample("mysql");
rs = dbMySQL.executeSelect("SELECT * FROM cars");
try
{
while( rs.next() )
{
String carModel = rs.getString( "model" );
System.out.println( carModel );
}
}
catch (SQLException e)
{
e.printStackTrace();
}
/* close connection */
dbMySQL.closeConnection();
/* *********************************************************************** */
/* Connect to PostgreSQL and get all users */
DBexample dbPg = new DBexample("pg");
rs = dbPg.executeSelect("SELECT * FROM users");
try
{
while( rs.next() )
{
String userName = rs.getString( "userName" );
System.out.println( userName );
}
}
catch (SQLException e)
{
e.printStackTrace();
}
/* close connection */
dbPg.closeConnection();
}
}
blogorama.com