"Karmanyevadhikaraste ma phaleshu kadachana, Ma karma phala hetur bhurmatey sangostva akarmani."

Java -data base connection

In order to connect java app to data base

1. Register the driver


Class.forName("sun.jdbc.odbc.jdbcodbcdriver")

2. Create  connection

Connection con= DriverManager.getConnection("jdbc:oracle:thin:@ localhost" ,"username", "password");

3.Create SQL statement

Statement s= con.createStatement();

4.Execute SQL statement

resultset rs= s.executeQuery("select * from user");
while(rs.Next())
System.out.println(rs.getString(1)

5.Close connection

con.close();




Access record from DB:

import java.sql.*;
public class Test
public static void main(String[] args)
{
try{


Class.forName("sun.jdbc.odbc.jdbcodbcdriver")
Connection con= DriverManager.getConnection("jdbc:oracle:thin:@ localhost" ,"username", "password");
Statement s= con.createStatement();

ResultSet rs= s.executeQuery("select * from user");
while(rs.Next()){
System.out.println(rs.getString(1);
con.close;
}catch( Exception e){
e.printStackTrace()
}

Insert record into DB:


import java.sql.*;
public class Test
public static void main(String[] args)
{
try{

Class.forName("sun.jdbc.odbc.jdbcodbcdriver")
Connection con= DriverManager.getConnection("jdbc:oracle:thin:@ localhost" ,"username", "password");
PreparedStatement ps= con.prepareStatement("insert into student Values("","");
ps.setInt(1,103)
ps.setString("meer","bb");
ps.executeUpdate();

con.close;
}catch( Exception e){
e.printStackTrace()
}