zoukankan      html  css  js  c++  java
  • JAVABEAN连接各数据库

    1.  连接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.  连接SQLSERVERSqlServerBean.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.  连接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()括号内可以加参数:

    ResultSet.TYPE_SCROLL_INSENSITIVE表示结果集可以滚动,但对数据库变化不敏感

    ResultSet.CONCUR_READ_ONLY表示结果集不能用于更新数据库

    ResultSet.TYPE_FORWARD_ONLY结果集不滚动

    ResultSet.TYPE_SCROLL_SENSITIVE结果集可以滚动,而且对数据库变化不敏感

    ResultSet.CONCUR_UPDATE可用于更新数据库

  • 相关阅读:
    vue中富文本编辑框
    vue中生成二维码
    在ABP中使用linq
    js根据年月得到当前这个月总共有多少天
    mescroll在vue中的应用
    javascript积累
    javascript常用的操作
    情侣间常犯的7个沟通问题
    欧洲旅游六大最佳目的地
    见与不见
  • 原文地址:https://www.cnblogs.com/webqiand/p/4315118.html
Copyright © 2011-2022 走看看