实现用java访问oracle数据库:
准备工作:
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的驱动,www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.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.");
}
}
这个是完全没有问题的。
先期准备:
1、两台电脑必须要联网,能够访问到
2、将oracle的驱动程序拷贝到你的java的电脑上,并设置到环境变量中。
java代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement prstm = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@ip:port:orcl"; //数据库连接字符串,需要替换ip,port,orcl
String user="user"; //用户名,需要替换你的数据库的用户名
String password="pswd"; //密码,需要替换为对应的密码
conn = DriverManager.getConnection(url,user,password); //获取连接
prstm = conn.prepareStatement("SELECT 1 TEST_ID FROM DUAL");
rs = prstm.executeQuery();
while (rs.next()) {
System.out.println("查询结果为:" + rs.getInt("TEST_ID"));
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//使用完毕进行相关内容的关闭,注意相关顺序
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (prstm != null) {
try {
prstm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
JDBC 去下java连Oracle的驱动,新建个java类,在里面写:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager("jdbc:oracle:thin:@localhost:1521:SID","USERNAME","PASSWORD");