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;
}
}
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;
}
}