JDBC (Java Databse Connectivity)
package : java.sql (API)
How to Connect DB with Java?
1- Create Data Soure Name (DSN)
JavaApplication -->JDBc-->DSN-->Driver(ODBC)-->Database
odbcad32
2- Java Application Steps
i) Load Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ii) Establish Connection with db
Connection con=
DriverManager.createConnection("jdbc:odbc:Lahore");
con.setAutoCommit(false);
iii)create Statement Object (for SQL)
Statement stmt= con.createStatement();
iv) run Query
String qry="insert into user values("Hafiz","123")"
stmt.executeUpdate(qry);
iv) Commit Transaction
con.commit();
........................................................
Example: Establish Connection
import java.sql.*;
import java.sql.*;
class MyDb{
private Connection con;
MyDb() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Lahore");
System.out.print("Connection Established ....");
}catch (Exception e ) { System.out.print(e);}
}
}
class Test{
public static void main(String o[]){
MyDb db=new MyDb();
}
}
.............................................................
Example: insert row
import java.sql.*;
import java.sql.*;
class MyDb{
private Connection con;
private Statement stmt;
MyDb() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Lahore");
stmt=con.createStatement();
con.setAutoCommit(false);
System.out.print("Connection Established ....");
}catch (Exception e ) { System.out.print(e);}
}
void insertRow(String username,String password){
String qry="insert into user values('"+username+"','"+password+"')";
try{
stmt.executeUpdate(qry);
con.commit();
System.out.print("Successfully Saved");
}catch (Exception e ) { System.out.print(e);}
}
}
class Test{
public static void main(String o[]){
MyDb db=new MyDb();
String username=o[0];
String password=o[1];
db.insertRow(username, password);
}
}
.........................................................................
Example: Retrive rows
import java.sql.*;
import java.sql.*;
class MyDb{
private Connection con;
private Statement stmt;
private ResultSet rs;
MyDb() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Lahore");
stmt=con.createStatement();
con.setAutoCommit(false);
System.out.print("Connection Established ....");
}catch (Exception e ) { System.out.print(e);}
}
void insertRow(String username,String password){
String qry="insert into user values('"+username+"','"+password+"')";
try{
stmt.executeUpdate(qry);
con.commit();
System.out.print("Successfully Saved");
}catch (Exception e ) { System.out.print(e);}
}
void retrieveRow(String qry){
try {
rs=stmt.executeQuery(qry);
while (rs.next()){
System.out.println(rs.getString("username") + "\t"+rs.getString("password"));
}
}catch (Exception e ) { System.out.print(e);}
}
}
class Test{
public static void main(String o[]){
MyDb db=new MyDb();
String qry=o[0];
db.retrieveRow(qry);
}
}
................................................................................
No comments:
Post a Comment