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”

*/

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Java database connection codes with MSACCESS , MYSQL, ORACLE

  1. JayswalG says:

    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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s