怎么实现用java远程访问oracle数据库
展开全部
先期准备:
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();
}
}
}
}
}
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();
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询