當前位置:首頁 » 操作系統 » oracle連接資料庫java

oracle連接資料庫java

發布時間: 2022-06-06 20:30:50

① 怎麼實現用java訪問oracle資料庫

用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.");
}
}

② 如何用Java實現連接Oracle

一個最簡單連接oracle的例子

package oracletest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleTest {
public static void main(String[] args) {
//String serverName = "localhost";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("1");
String url = "jdbc:oracle:thin:@localhost:1521:ORA9I";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
System.out.println("2");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test1");
while (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}

③ java怎麼連接oracle資料庫

第一種方法JDBC鏈接:
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "xxxxx";
String password = "xxxxxx";
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();

}
return connection;
}

第二種Spring中Bean配置:
<!-- 配置oracle數據源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value ="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="xxxxxx" />
<property name="password" value="xxxxxxxxx" />
</bean>

④ java與oracle要怎樣連接

JDBC啊,六大步,
String url = "jdbc:oracle:thin:@localhost:1521:XE";//固定的,照著寫就行
String username = "system";//oracle用戶名
String pwd = "123";//oracle密碼
String sql = "select sum(cost) from orders where userid = ? and orderstatusid = 3";
String sql2 = "select * from vidmember where id = ?";

/*
* 根據id查找出該人消費的總金額。
* */
public int MoneyfindById(int id) {

int a = 0;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, pwd);
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
rs = pst.executeQuery();
if(rs.next()){
a = rs.getInt("sum(cost)");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return a;
}

⑤ 怎樣使用java 連接oracle資料庫

Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");//加入oracle的驅動,「」裡面是驅動的路徑

String url = "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL";
/*資料庫連接,oracle代表鏈接的是oracle資料庫;thin:@MyDbComputerNameOrIP代表的是資料庫所在的IP地址(可以保留thin:);1521代表鏈接資料庫的埠號;ORCL代表的是資料庫名稱*/

String UserName = "root";// 資料庫用戶登陸名 ( 也有說是 schema 名字的 )

String Password = "2006";// 密碼

conn = DriverManager.getConnection(url, UserName, Password);

熱點內容
怎麼加密伺服器上的文檔 發布:2025-01-09 05:56:22 瀏覽:465
安卓80跟90哪個好用 發布:2025-01-09 05:55:28 瀏覽:333
原力文件夾 發布:2025-01-09 05:51:44 瀏覽:125
php寫入文本 發布:2025-01-09 05:45:00 瀏覽:879
考研編程作品 發布:2025-01-09 05:35:00 瀏覽:332
安卓相冊哪個好看 發布:2025-01-09 05:16:01 瀏覽:983
java分析數據 發布:2025-01-09 05:16:00 瀏覽:853
視頻md5加密 發布:2025-01-09 05:08:59 瀏覽:927
xp系統文件夾加密 發布:2025-01-09 04:52:38 瀏覽:172
外部調用shell腳本內函數 發布:2025-01-09 04:49:14 瀏覽:256