zoukankan      html  css  js  c++  java
  • 各种数据库连接代码的测试类(java)

    测试类:

     public class Mytest {
             Connection conn=null;
          Statement stmt=null;
          String myDriver="com.mysql.jdbc.Driver";
          String url="jdbc:mysql://127.0.0.1:3306/game";
          public Mytest()
           {
                    try {
             Class.forName(myDriver);
                System.out.println("驱动加裁成功");
                } catch (ClassNotFoundException e) {
             e.printStackTrace();
             }
            try{
             conn= DriverManager.getConnection(url,"root","root");
             System.out.println("连接成功");
             stmt= conn.createStatement();
              }catch(SQLException e){
             e.printStackTrace();
             }
               }  
          public ResultSet query(String sql)
           {
            try {
             return stmt.executeQuery(sql);
                } catch (SQLException e) {  
              e.printStackTrace();
              return null;
             }
               }
          public void insert(String sql)
           {
            try {
             stmt.execute(sql);
            } catch (SQLException e) {  
              e.printStackTrace();
             }
           }
          public void delete(String sql)
           {
            try {
             stmt.execute(sql);
               } catch (SQLException e) {  
              e.printStackTrace();
             }
           }
          public void update(String sql)
           {
            try {
             stmt.execute(sql);
               } catch (SQLException e) {
              e.printStackTrace();
             }
           }
             }

    连接池配置

    MYSQL(mysql-connector-java-3.1.10-bin.jar):
     
         package databaseoper;
     
         import java.sql.*;
     
         import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
     
         public class SqlserverSingletonDataSource {
              static private MysqlConnectionPoolDataSource ds;
             private SqlserverSingletonDataSource() {
              ds = new MysqlConnectionPoolDataSource();
               ds.setServerName("localhost");
               ds.setDatabaseName("dba");
              ds.setUser("root");
               ds.setPassword("sa");
        }
     
        public static Connection getConnection() {
            if (ds == null) {
                new SqlserverSingletonDataSource();
            }
            Connection con = null;
            try {
                con = ds.getConnection();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return con;
        }
        public static void main(String []arg)
        {
     
           System.out.print("123");
          Connection co=getConnection();
            System.out.print(co);
            System.out.print("123");
        }
    }
     
    ORACLE(ojdbc14.jar):
     
     
    package newanli;
     
    import oracle.jdbc.pool.OracleDataSource;
    import java.sql.SQLException;
    import java.sql.Connection;
     
    public class DatabaseConnection {
        static private OracleDataSource ods;
         private DatabaseConnection() {
             if(ods==null)
             {
                 try {
                     ods = new OracleDataSource();
                     ods.setURL("jdbc:oracle:thin:@localhost:1521:orcl");
                     ods.setUser("scott");
                     ods.setPassword("tiger");
                 } catch (SQLException ex) {
                 }
             }
         }
         public static Connection getConnection()
         {
             if(ods==null)
             {
                 new DatabaseConnection();
             }
             Connection con=null;
             try {
                 con = ods.getConnection();
             } catch (SQLException ex) {
             }
             return con;
         }
     
    }
     
    MMSQL(jtds-1.2.jar):
     
    package databaseoper;
     
    import java.sql.*;
    import net.sourceforge.jtds.jdbcx.JtdsDataSource;
     
    public class SqlserverSingletonDataSource {
        static private JtdsDataSource ds;
        private SqlserverSingletonDataSource() {
            ds = new JtdsDataSource();
            ds.setServerName("localhost");
            ds.setDatabaseName("myProject");
            ds.setUser("sa");
            ds.setPassword("sa");
        }
     
        public static Connection getConnection() {
            if (ds == null) {
                new SqlserverSingletonDataSource();
            }
            Connection con = null;
            try {
                con = ds.getConnection();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return con;
        }
    }

    之前所写,迁移至此

    原文链接:http://user.qzone.qq.com/372806800/blog/1336200772

  • 相关阅读:
    矩阵树定理 / 生成树计数
    NC20811 蓝魔法师 (树形DP/树上01背包)
    Xor-MST学习/ 2020牛客暑假多校(五)B.Graph
    HDU-6820 Tree (2020杭电多校(五) 1007)
    Flipping Coins (概率DP)
    宝石装箱 容斥+dp
    Rabbit的工作(1) (dP)
    Codeforces-1350 E.Orac and Game of Life
    HDU-6563 Strength (贪心)
    HDU-6558 The Moon (期望DP)
  • 原文地址:https://www.cnblogs.com/amwuau/p/6235980.html
Copyright © 2011-2022 走看看