JavaBean Connecting each database

Source: Internet
Author: User
Tags stmt

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.