Wednesday, July 6, 2011

Java database connection codes with MSACCESS , MYSQL, ORACLE

MSACCESS
-----------------


/* Java And MsAccess*/

import java.sql.*;
import java.io.*;

class MsAccessConnection
{

public static void main(String rk[])
{
Connection con;
Statement st;
ResultSet rs=null;

try {
jdbc.forname("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:table.DatabaseName","username","password");
st=con.Statement();
rs=st.executeQuery("select * from tablename");
while(rs.next())
{
System.out.println(rs.getString(1));
}

}catch(Exception e)
{
System.out.println("Error : "+e);
}

}
}

/*

Note :
Control panel -> administrative tools -> data sources(ODBC) ->
STEP 1: user DSN -> ADD -> Driver do Microsoft Access (*.mdb) -> finish -> select -> data source name : (only database name without extension)
description : (only database name without extension) -> ok

Same Process for System DSN

*/
--------------------------------------------------------------------

MYSQL
------------

/* java and mysql */


import java.sql.*;
import java.io.*;

public class MysqlConnection
{
public static void main(String[] rk)
{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;

try
{

Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/DatabaseName","username","password");

stmt=con.createStatement();

rs=stmt.executeQuery("select * from contacts");


while(rs.next())
{
System.out.println(rs.getString(1));
}

}catch(Exception e)
{
System.out.println("Error : "+e);
}
}

}

/* Copy your MySql Connector .jar file into "C:\Program Files\Java\jdk1.6.0_06\jre\lib\ext" location or set the classpath for the same .jar file*/
/* Create one user into your MySql Server or use the default user name and password (username="root password="root") */


ORACLE
-------------


/*Java and ORACLE */

import java.sql.*;

class OracleConnection
{
public static void main(String[] rk)
{

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.100.10:1521/Stud10g", "username", "password");

/*
give ip address of your server machine or if oracel is installed in your stand alone machine then given localhost instead of ip
address
Stud10g is database name
*/

Statement stmt=conn.createStatement();
String query="select * from contacts";
ResultSet rs=stmt.executeQuery(query);

while(rs.next())
{
System.out.println(rs.getString(1));
}

}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}


/*
Note :
Download the required version of driver and place it into the below directory path "C:\Oracle\product\10.1.0\Client_1\jdbc\lib"

*/

1 comment:

  1. This one is amazing for those who really dont know how to use database with Java...
    Because in .net there is ADODB and ADODC objects are doing this work very easily but for Java you need to write a code at least for once to connect with the data base..
    Thanx for sharing this info...

    ReplyDelete