当前位置: 技术问答>java相关
我需要JAVA-JDBC打开数据库的例子....................
来源: 互联网 发布时间:2015-02-26
本文导语: 1:打开DB2的,2:打开SQL Server的,3:打开Oracle的,4:打开Informix的. [oracle] jdbc.oracle.driver.OracleDriver jdbc:oracle:thin@host(主机名):1521:ORCL [db2] driver=COM.ibm.db2.jdbc.app.DB2Driver dbUrl=jdbc:db2:dbname [sql server] driver=sun.jdbc.odbc.JdbcOdbcDriver dbUrl=jd...
1:打开DB2的,2:打开SQL Server的,3:打开Oracle的,4:打开Informix的.
[oracle]
jdbc.oracle.driver.OracleDriver
jdbc:oracle:thin@host(主机名):1521:ORCL
[db2]
driver=COM.ibm.db2.jdbc.app.DB2Driver
dbUrl=jdbc:db2:dbname
[sql server]
driver=sun.jdbc.odbc.JdbcOdbcDriver
dbUrl=jdbc:odbc:dbname
上面是我从书上看到的,但是写的代码通不过。谁能帮我写的详细一点(实例化:返回数据库(某种)Db1下的Tb1表的第一条记录)。要求:不用建数据源。直接用代码操作。
[oracle]
jdbc.oracle.driver.OracleDriver
jdbc:oracle:thin@host(主机名):1521:ORCL
[db2]
driver=COM.ibm.db2.jdbc.app.DB2Driver
dbUrl=jdbc:db2:dbname
[sql server]
driver=sun.jdbc.odbc.JdbcOdbcDriver
dbUrl=jdbc:odbc:dbname
上面是我从书上看到的,但是写的代码通不过。谁能帮我写的详细一点(实例化:返回数据库(某种)Db1下的Tb1表的第一条记录)。要求:不用建数据源。直接用代码操作。
|
package com.west163.wlywest.test;
import java.sql.*;
public class JdbcCon{
public JdbcCon(){
}
public java.sql.ResultSet ExcuteSQL(String sSQL){
java.sql.ResultSet res;
res = null;
try{
System.out.println("Registering JDBC driver...");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to the database...");
Connection jdbclink = DriverManager.getConnection("jdbc:oracle:thin:@uisqds029:1521:orcl","system","manager");
System.out.println("Connect OK...");
Statement stament = jdbclink.createStatement();
System.out.println("Executing Query...");
res = stament.executeQuery(sSQL);
}
catch(SQLException ex){
}
return res;
}
}
import java.sql.*;
public class JdbcCon{
public JdbcCon(){
}
public java.sql.ResultSet ExcuteSQL(String sSQL){
java.sql.ResultSet res;
res = null;
try{
System.out.println("Registering JDBC driver...");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to the database...");
Connection jdbclink = DriverManager.getConnection("jdbc:oracle:thin:@uisqds029:1521:orcl","system","manager");
System.out.println("Connect OK...");
Statement stament = jdbclink.createStatement();
System.out.println("Executing Query...");
res = stament.executeQuery(sSQL);
}
catch(SQLException ex){
}
return res;
}
}
|
呵呵,看过加菲猫的都知道,那个是真正的主人。