JDBC = JAVA DATABASE CONNECTIVITY
- Using JDBC concept we can make a connection between java and different databases. Basically there are four types of drivers exits for the database connectivity.drivers are- TYPE-1,TYPE-2,TYPE-3,TYPE-4.
- In Below program bold syntax indicates these are the main statement to establish connectivity with database.
- In this program, we use the JdbcOdbcDriver for connectivity.
Program:-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;
public class Conn {
public static void main(String[] args)
throws ClassNotFoundException,SQLException, IOException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:XE");
Statement st=cn.createStatement();
System.out.println("Insert Data..");
String query="insert into Bank_Account
values('A101','Raj',1500,9375974438,'220,mumbai')";
int i=st.executeUpdate(query);
System.out.println("Update Data..");
query="update Bank_Account set Balance=20000 where AccountNo='A-101'";
i=st.executeUpdate(query);
System.out.println("Delete Data..");
query="delete * from Bank_Account where AccountNo='A-101'";
i=st.executeUpdate(query);
System.out.println("Data are..");
ResultSet
res=st.executeQuery("select * from Bank_Account");
while(res.next()){
System.out.println("AccountNo
: " + res.getString(1));
System.out.println("CustomerName : " + res.getString(2));
System.out.println("Balance :
" + res.getInt(3));
System.out.println("Phone No :
" + res.getString(4));
System.out.println("Address :
" + res.getString(5));
}
cn.close();
}
Output:-
0 comments:
Post a Comment