如何使用JDBC连接oracle数据库
2个回答
2016-01-13 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
1)首先要有java连接oracle数据库的驱动类,下载好后导入到工程里
2)代码:
import java.sql.*;
public class JDBCTest {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args)
throws ClassNotFoundException, SQLException{
//1.准备参数
String ip = "192.168.0.26";
String sid = "tarena";
String port = "1521";
String dbUser = "openlab";
String dbPassword = "open123";
String driver
= "oracle.jdbc.driver.OracleDriver";
//2.构造驱动实例
Class.forName(driver);
//3.创建连接
//连接字符串是固定的形式,oracle的形式:
String url
= "jdbc:oracle:thin:@"
+ ip + ":" + port + ":" + sid;
Connection conn
= DriverManager.getConnection
(url, dbUser, dbPassword);
//4.执行SQL语句
String sql = "select id, password, name from user_sd1104 " +
"where id = 1001 and password = '1234'";
Statement stmt = conn.createStatement();
ResultSet rs
= stmt.executeQuery(sql);//执行sql语句
while(rs.next()){
int id = rs.getInt(1);
String pwd = rs.getString(2);
String name = rs.getString(3);
System.out.println("welcome," + name);
}
rs.close();
stmt.close();
conn.close();
}
}
2)代码:
import java.sql.*;
public class JDBCTest {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args)
throws ClassNotFoundException, SQLException{
//1.准备参数
String ip = "192.168.0.26";
String sid = "tarena";
String port = "1521";
String dbUser = "openlab";
String dbPassword = "open123";
String driver
= "oracle.jdbc.driver.OracleDriver";
//2.构造驱动实例
Class.forName(driver);
//3.创建连接
//连接字符串是固定的形式,oracle的形式:
String url
= "jdbc:oracle:thin:@"
+ ip + ":" + port + ":" + sid;
Connection conn
= DriverManager.getConnection
(url, dbUser, dbPassword);
//4.执行SQL语句
String sql = "select id, password, name from user_sd1104 " +
"where id = 1001 and password = '1234'";
Statement stmt = conn.createStatement();
ResultSet rs
= stmt.executeQuery(sql);//执行sql语句
while(rs.next()){
int id = rs.getInt(1);
String pwd = rs.getString(2);
String name = rs.getString(3);
System.out.println("welcome," + name);
}
rs.close();
stmt.close();
conn.close();
}
}
今至电子科技有限公司
2024-08-23 广告
2024-08-23 广告
数据库备份是确保数据安全与业务连续性的关键环节。我们上海今至电子科技有限公司高度重视数据保护,定期执行全面的数据库备份策略。这包括使用先进工具和技术,对关键业务数据进行自动化备份,并存储在安全可靠的外部存储介质或云端。通过定期验证备份的完整...
点击进入详情页
本回答由今至电子科技有限公司提供
展开全部
import java.sql.*;
public class MyJDBC {
public MyJDBC() {
}
public static String getConnectionUrl(){
return "jdbc:oracle:thin:@DEREK:1521:OracleDB";
}
public static Connection getConnection(){
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(getConnectionUrl(),"dengzhao", "780128");
if (con != null){
System.out.println("连接成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void closeConnetion(ResultSet rs, Statement stmt, Connection con){
try {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (con != null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement();
String sql = "select * from students";
rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeConnetion(rs, stmt, con);
}
}
}
public class MyJDBC {
public MyJDBC() {
}
public static String getConnectionUrl(){
return "jdbc:oracle:thin:@DEREK:1521:OracleDB";
}
public static Connection getConnection(){
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(getConnectionUrl(),"dengzhao", "780128");
if (con != null){
System.out.println("连接成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void closeConnetion(ResultSet rs, Statement stmt, Connection con){
try {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (con != null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = getConnection();
stmt = con.createStatement();
String sql = "select * from students";
rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeConnetion(rs, stmt, con);
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询