• JAVABEAN连接各数据库

    2015-03-05 521阅读 0评论

    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可用于更新数据库

    posted @   webnote  阅读(521)  评论(0编辑  收藏  举报
    我要评论

    编辑推荐:
    · 深入理解 Mybatis 分库分表执行原理
    · 如何打造一个高并发系统?
    · .NET Core GC压缩(compact_phase)底层原理浅谈
    · 现代计算机视觉入门之:什么是图片特征编码
    · .NET 9 new features-C#13新的锁类型和语义
    阅读排行:
    · Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
    · 语音处理 开源项目 EchoSharp
    · 《HelloGitHub》第 106 期
    · Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
    · 使用 Dify + LLM 构建精确任务处理应用
    点击右上角即可分享
    微信分享提示