1.下载驱动,打开浏览器,搜索oracle数据库驱动,进入下载页面下载即可;
2.将驱动导入到项目中。
用java访问oracle数据库代码实现:
try
{
Class.forName(
"oracle.jdbc.driver.OracleDriver"
);
//加载驱动
//与数据库建立连接
try
{
String url =
"jdbc:oracle:thin:@localhost:1521:orcl"
;
String user =
"scott"
;
//用户名
String password =
"tiger"
;
//密码
conn=DriverManager.getConnection(url,username,password);
}
catch
(SQLException e) {
e.printStackTrace();
}
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
用JDBC啊,
首先去oracle网站下个java连接oracle的驱动,http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
代码列子:
import java.sql.*;
public class TestDBOracle {
public static void main(String[] args)
throws ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
//
// or
// DriverManager.registerDriver
// (new oracle.jdbc.driver.OracleDriver());
String url = "jdbc:oracle:thin:@//server.local:1521/prod";
// jdbc:oracle:thin:@//host:port/service
// or
// String url = "jdbc:oracle:thin:@server.local:1521:prodsid";
// jdbc:oracle:thin:@host:port:SID
Connection conn =
DriverManager.getConnection(url,"scott", "tiger");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset =
stmt.executeQuery("select BANNER from SYS.V_$VERSION");
while (rset.next()) {
System.out.println (rset.getString(1));
}
stmt.close();
System.out.println ("Ok.");
}
}