编程论坛
注册
登录
编程论坛
→
J2EE论坛
[求助] 谁知道在MyEclipse中直联oracle方法!
j2ee126
发布于 2008-07-18 14:50, 1881 次点击
我想在MyEclipse中写一个直联oracle的方法,怎么样写啊!!
7 回复
#2
ming206
2008-07-18 16:25
直联?
#3
j2ee126
2008-07-18 17:25
对啊,就是不用连接池、也不用数据源,而是直接与数据库的方法!
#4
guoxhvip
2008-07-18 22:09
用jdbc不就行了
#5
zengqingcong
2008-07-18 22:41
package util;
import java.sql.*;
public class ConnectionDB {
private static Connection conn = null;
private static String driver = null;
private static String url = null;
private static String user = null;
private static String pass = null;
private ConnectionDB() {
}
public static Connection getOracleCon() {
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:itstar";// itstarΪ��ݿ����
user = "itstar";
pass = "itstar";
try {
Class.forName(driver);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
if (conn == null) {
conn = DriverManager.getConnection(url, user, pass);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Connection getSqlCon() {
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbName";// dbNameΪ��ݿ����
user = "sa";
pass = "sa";
try {
Class.forName(driver);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
if (conn == null) {
conn = DriverManager.getConnection(url, user, pass);
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
Connection connection = ConnectionDB.getOracleCon();
try {
Statement state = connection.createStatement();
ResultSet rs = state.executeQuery("select * from users");
// ResultSet rs = state
// .executeQuery("insert into usersvalues('liu','123456')");
while (rs.next()) {
System.err.println(rs.getString(1));
System.err.println(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
#6
j2ee126
2008-07-19 08:34
在使用直联的时候,是不是需要一些.jar文件啊!发我邮箱:j2ee126@ ,谢谢!
#7
guoxhvip
2008-07-19 22:40
在oracle的安装目录下有个classes12.jar 就是这个 还需要传?
#8
j2ee126
2008-07-21 09:19
问题已解决,oracle直联jar包我用的是ojdbc14.jar.
1