Eclipse中JDBC各数据库怎样连接
2个回答
展开全部
常用JDBC连接数据库方法总结如下:
一、JDBC连接DB2
Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://dburl:port/DBname"
cn = DriverManager.getConnection( url, sUsr, sPwd );
二、JDBC连接Microsoft SQLServer(microsoft)
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master", sUsr, sPwd );
三、JDBC连接Sybase(jconn2.jar)
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
cn = DriverManager.getConnection( "jdbc:sybase:Tds:DBServerIP:2638", sUsr, sPwd );
四、JDBC连接MySQL(mm.mysql-3.0.2-bin.jar)
Class.forName( "org.gjt.mm.mysql.Driver" );
cn = DriverManager.getConnection( "jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd );
五、JDBC连接PostgreSQL(pgjdbc2.jar)
Class.forName( "org.postgresql.Driver" );
cn = DriverManager.getConnection( "jdbc:postgresql://DBServerIP/myDatabaseName", sUsr, sPwd );
六、JDBC连接Oracle(classes12.jar)
Class.forName( "oracle.jdbc.driver.OracleDriver" );
cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
七、JDBC连接ODBC
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );
一、JDBC连接DB2
Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");
String url="jdbc:db2://dburl:port/DBname"
cn = DriverManager.getConnection( url, sUsr, sPwd );
二、JDBC连接Microsoft SQLServer(microsoft)
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master", sUsr, sPwd );
三、JDBC连接Sybase(jconn2.jar)
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );
cn = DriverManager.getConnection( "jdbc:sybase:Tds:DBServerIP:2638", sUsr, sPwd );
四、JDBC连接MySQL(mm.mysql-3.0.2-bin.jar)
Class.forName( "org.gjt.mm.mysql.Driver" );
cn = DriverManager.getConnection( "jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd );
五、JDBC连接PostgreSQL(pgjdbc2.jar)
Class.forName( "org.postgresql.Driver" );
cn = DriverManager.getConnection( "jdbc:postgresql://DBServerIP/myDatabaseName", sUsr, sPwd );
六、JDBC连接Oracle(classes12.jar)
Class.forName( "oracle.jdbc.driver.OracleDriver" );
cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
七、JDBC连接ODBC
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection cn = DriverManager.getConnection( "jdbc:odbc:" + sDsn, sUsr, sPwd );
展开全部
public static Connection getOrclCn() {
Connection cn = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl3";
cn=DriverManager.getConnection(url, "scott", "tiger");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public static Connection getMysqlCn() {
Connection cn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
cn=DriverManager.getConnection(url, "root", "root");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public static Connection getSql2000Cn() {
Connection cn = null;
try {//jdbc:mysql://localhost:3306/test
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=DB_111201_A5";
cn=DriverManager.getConnection(url, "sa", "sa");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public static Connection getSql2005Cn() {
Connection cn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=DB_111201_A5";
cn = DriverManager.getConnection(url,"sa", "sa");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public Connection getAccessCn(){
Connection cn=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String a=getClass().getResource("/res/test.mdb").getPath().substring(1);
a=URLDecoder.decode(a, "utf-8");
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+a;
cn=DriverManager.getConnection(url,"","");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}finally{
Cn.closeCn(cn);
}
return cn;
}
Connection cn = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl3";
cn=DriverManager.getConnection(url, "scott", "tiger");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public static Connection getMysqlCn() {
Connection cn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
cn=DriverManager.getConnection(url, "root", "root");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public static Connection getSql2000Cn() {
Connection cn = null;
try {//jdbc:mysql://localhost:3306/test
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=DB_111201_A5";
cn=DriverManager.getConnection(url, "sa", "sa");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public static Connection getSql2005Cn() {
Connection cn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=DB_111201_A5";
cn = DriverManager.getConnection(url,"sa", "sa");
} catch (java.lang.ClassNotFoundException classnotfound) {
classnotfound.printStackTrace();
} catch (java.sql.SQLException sql) {
sql.printStackTrace();
}
return cn;
}
public Connection getAccessCn(){
Connection cn=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String a=getClass().getResource("/res/test.mdb").getPath().substring(1);
a=URLDecoder.decode(a, "utf-8");
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+a;
cn=DriverManager.getConnection(url,"","");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}finally{
Cn.closeCn(cn);
}
return cn;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询