當前位置:首頁 » 操作系統 » 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);

熱點內容
除法C編程 發布:2024-10-31 19:10:30 瀏覽:963
id管理員密碼是什麼 發布:2024-10-31 19:09:07 瀏覽:305
設置資料庫 發布:2024-10-31 19:09:06 瀏覽:324
怎麼查看海信u7的配置 發布:2024-10-31 18:59:53 瀏覽:708
國企演算法工資 發布:2024-10-31 18:58:15 瀏覽:117
雲視通密碼怎麼修改 發布:2024-10-31 18:58:12 瀏覽:128
怎麼讓軟體訪問通訊錄 發布:2024-10-31 18:52:36 瀏覽:117
貼吧訪問量 發布:2024-10-31 18:28:54 瀏覽:887
visio在什麼配置中可以用 發布:2024-10-31 18:03:21 瀏覽:552
寶塔伺服器讓手機主題和電腦不同 發布:2024-10-31 18:01:44 瀏覽:241