各种数据库连接代码的测试类(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

posted @ 2016-12-30 10:52  风来风往风伤(TiAmo)  阅读(5431)  评论(0编辑  收藏  举报