1. Connect to access( Accessbean.java)
Package access;
Import java.sql.*;
public class Accessbean
{
String driver= "Sun.jdbc.odbc.JdbcOdbcDriver";
Connection Connect=null;
ResultSet Rs=null;
Public Accessbean () {
try{
Class.forName (driver);
}catch (Exception e) {
E.printstacktrace ();
}
}
Public ResultSet query (String sql) {
try{
Connect=drivermanager.getconnection ("Jdbc:odbc:student", "", "" ");
Statement stmt=connect.createstatement (resultset.type_scroll_insensitive,resultset.concur_read_only);
Rs=stmt.executequery (SQL);
}catch (Exception e) {
E.printstacktrace ();
}
Return RS;
}
public void update (String sql) {
try{
Connect=drivermanager.getconnection ("Jdbc:odbc:vote", "", "" ");
Statement stmt=connect.createstatement ();
Stmt.executeupdate (SQL);
}catch (Exception e) {
E.printstacktrace ();
}
}
};
2. connecting to SQL Server(sqlserverbean.java)
Package SQL;
Import java.sql.*;
public class Sqlserverbean
{
String driver= "Com.microsoft.jdbc.sqlserver.SQLServerDriver";
Connection Connect=null;
ResultSet Rs=null;
Public Sqlserverbean () {
try{
Class.forName (driver);
}catch (Exception e) {
E.printstacktrace ();
}
}
Public ResultSet query (String sql) {
try{
Connect=drivermanager.getconnection ("Jdbc:microsoft:sqlserver://ts06:1433;database Name=mydata", "sa", "sa");
Statement stmt=connect.createstatement ();
Rs=stmt.executequery (SQL);
}catch (Exception e) {
E.printstacktrace ();
}
Return RS;
}
public void update (String sql) {
try{
Connect=drivermanager.getconnection ("Jdbc:microsoft:sqlserver://ts06:1433;databasename=mydata", "sa", "sa");
Statement stmt=connect.createstatement ();
Stmt.executeupdate (SQL);
}catch (Exception e) {
E.printstacktrace ();
}
}
};
3. Connect ORACLE
Package Oracle;
Import java.sql.*;
public class Oraclebean
{
String servername= "localhost";
String driver= "Oracle.jdbc.driver.OracleDriver";
String url= "jdbc:oracle:thin:@" +servername+ ": 1521:oemrep";
Connection Connect=null;
ResultSet Rs=null;
Public Oraclebean () {
try{
Class.forName (driver);
}catch (Exception e) {
E.printstacktrace ();
}
}
Public ResultSet query (String sql) {
try{
Connect=drivermanager.getconnection (URL, "System", "manager");
Statement stmt=connect.createstatement ();
Rs=stmt.executequery (SQL);
}catch (Exception e) {
E.printstacktrace ();
}
Return RS;
}
public void update (String sql) {
try{
Connect=drivermanager.getconnection (URL, "System", "manager");
Statement stmt=connect.createstatement ();
Stmt.executeupdate (SQL);
}catch (Exception e) {
E.printstacktrace ();
}
}
};
Statement stmt=connect.createstatement () can be added in parentheses:
resultset.type_scroll_insensitive indicates that the result set can be scrolled but not sensitive to database changes
resultset.concur_read_only indicates that the result set cannot be used to update the database
resultset.type_forward_only result set does not scroll
resultset.type_scroll_sensitive result set can be scrolled and not sensitive to database changes
resultset.concur_update can be used to update the database
JavaBean Connecting each database