各种数据库连接代码

SqlServer2000
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tempdb";
SqlServer2005
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
URL = "jdbc:sqlserver://localhost:1433;DatabaseName=tempdb";
oracle连库
String myDriver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:dba";
Class.forName(myDriver);
System.out.print("驱动加载成功!");
conn=DriverManager.getConnection(url, "scott", "tiger");
System.out.println("连接成功!");
stmt=conn.createStatement();
mysql连库
String myDriver="com.mysql.jdbc.Driver";
String myDriver="org.git.mm.mysql.Driver";
String url="jdbc:mysql://127.0.0.1:3306/game";
conn= DriverManager.getConnection(url,"root","root");

测试类
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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值