⑴ java 連接oracle
應該是你的連接字元串的問題,你看一下這個鏈接的內容
http://..com/question/161407728.html
⑵ 如何用Java實現連接Oracle
1、工作環境:myeclipse中->新建java_oracle工程->新建包com.zp->新建java_oracle_jdbc.java類
2、需要配置Oracle資料庫驅動
java工程(java_oracle)—>右鍵屬性(Properties)->Java Build Path->Libraries->Add External JARs->添加oracle安裝目錄E:\oracle\proct\10.1.0\Db_1\jdbc\lib下的「classes12.jar」文件;
每次新建一個不同工程都需要配置一次
3、代碼如下:
package com.zp;
import java.sql.*;
public class java_oracle_jdbc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
//載入驅動
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到連接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:simlink", "scott", "tiger");
Statement sm=ct.createStatement();
ResultSet rs=sm.executeQuery("select * from emp");
while(rs.next())
{
System.out.println("用戶名:"+rs.getString(2));
}
rs.close();
sm.close();
ct.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
⑶ java怎麼調用oracle的過程
java下實現調用oracle的存儲過程和函數
在oracle下創建一個test的賬戶,然後按一下步驟執行:
1.創建表:STOCK_PRICES
--創建表格
CREATE TABLE STOCK_PRICES(
RIC VARCHAR(6) PRIMARY KEY,
PRICE NUMBER(7,2),
UPDATED DATE );
2.插入測試數據:
--插入數據
INSERT INTO stock_prices values('1111',1.0,SYSDATE);
INSERT INTO stock_prices values('1112',2.0,SYSDATE);
INSERT INTO stock_prices values('1113',3.0,SYSDATE);
INSERT INTO stock_prices values('1114',4.0,SYSDATE);
3.建立一個返回遊標: PKG_PUB_UTILS
--建立一個返回遊標
CREATE OR REPLACE PACKAGE PKG_PUB_UTILS IS
--動態游標
TYPE REFCURSOR IS REF CURSOR;
END PKG_PUB_UTILS;
4.創建和存儲過程:P_GET_PRICE
--創建存儲過程
CREATE OR REPLACE PROCEDURE P_GET_PRICE
(
AN_O_RET_CODE OUT NUMBER,
AC_O_RET_MSG OUT VARCHAR2,
CUR_RET OUT PKG_PUB_UTILS.REFCURSOR,
AN_I_PRICE IN NUMBER
)
IS
BEGIN
AN_O_RET_CODE := 0;
AC_O_RET_MSG := '操作成功';
OPEN CUR_RET FOR
SELECT * FROM STOCK_PRICES WHERE PRICE<AN_I_PRICE;
EXCEPTION
WHEN OTHERS THEN
AN_O_RET_CODE := -1;
AC_O_RET_MSG := '錯誤代碼:' || SQLCODE || CHR(13) || '錯誤信息:' || SQLERRM;
END P_GET_PRICE;
5.創建函數:
--創建函數:F_GET_PRICE
CREATE OR REPLACE FUNCTION F_GET_PRICE(v_price IN NUMBER)
RETURN PKG_PUB_UTILS.REFCURSOR
AS
stock_cursor PKG_PUB_UTILS.REFCURSOR;
BEGIN
OPEN stock_cursor FOR
SELECT * FROM stock_prices WHERE price < v_price;
RETURN stock_cursor;
END;
6.JAVA調用存儲過程返回結果集
代碼示例:JDBCoracle10G_INVOKEPROCEDURE.java
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
/* 本例是通過調用oracle的存儲過程來返回結果集:
* oracle 9i、10G 的jdbc由1個jar包組成:classes12.zip
*/
public class JDBCoracle10G_INVOKEPROCEDURE {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
CallableStatement stmt = null;
String driver;
String url;
String user;
String pwd;
String sql;
String in_price;
public JDBCoracle10G_INVOKEPROCEDURE()
{
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:ORCL";
// oracle 用戶
user = "test";
// oracle 密碼
pwd = "test";
init();
// mysid:必須為要連接機器的sid名稱,否則會包以下錯:
// java.sql.SQLException: Io 異常: Connection
// refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
// 參考連接方式:
// Class.forName( "oracle.jdbc.driver.OracleDriver" );
// cn = DriverManager.getConnection(
// "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
}
public void init() {
System.out.println("oracle jdbc test");
try {
Class.forName(driver);
System.out.println("driver is ok");
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("conection is ok");
statement = conn.createStatement();
// conn.setAutoCommit(false);
// 輸入參數
in_price = "3.0";
// 調用函數
stmt = conn.prepareCall("call P_GET_PRICE(?,?,?,?)");
stmt.registerOutParameter(1, java.sql.Types.FLOAT);
stmt.registerOutParameter(2, java.sql.Types.CHAR);
stmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(4, in_price);
stmt.executeUpdate();
int retCode = stmt.getInt(1);
String retMsg = stmt.getString(2);
if (retCode == -1) { // 如果出錯時,返回錯誤信息
System.out.println("報錯!");
} else {
// 取的結果集的方式一:
rs = ((OracleCallableStatement) stmt).getCursor(3);
// 取的結果集的方式二:
// rs = (ResultSet) stmt.getObject(3);
String ric;
String price;
String updated;
// 對結果進行輸出
while (rs.next()) {
ric = rs.getString(1);
price = rs.getString(2);
updated = rs.getString(3);
System.out.println("ric:" + ric + ";-- price:" + price
+ "; --" + updated + "; ");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("close ");
}
}
public static void main(String args[])// 自己替換[]
{
new JDBCoracle10G_INVOKEPROCEDURE();
}
}
7.開發JAVA調用函數返回結果集
代碼示例:JDBCoracle10G_INVOKEFUNCTION.java
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
/*
/* 本例是通過調用oracle的函數來返回結果集:
* oracle 9i、10G 的jdbc由1個jar包組成:classes12.zip
*/
public class JDBCoracle10G_INVOKEFUNCTION {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
CallableStatement stmt = null;
String driver;
String url;
String user;
String pwd;
String sql;
String in_price;
public JDBCoracle10G_INVOKEFUNCTION()
{
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:ORCL";
// oracle 用戶
user = "test";
// oracle 密碼
pwd = "test";
init();
// mysid:必須為要連接機器的sid名稱,否則會包以下錯:
// java.sql.SQLException: Io 異常: Connection
// refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
// 參考連接方式:
// Class.forName( "oracle.jdbc.driver.OracleDriver" );
// cn = DriverManager.getConnection(
// "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
}
public void init() {
System.out.println("oracle jdbc test");
try {
Class.forName(driver);
System.out.println("driver is ok");
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("conection is ok");
statement = conn.createStatement();
// conn.setAutoCommit(false);
// 輸入參數
in_price = "5.0";
// 調用函數
stmt = conn.prepareCall("{? = call F_GET_PRICE(?)}");
// stmt.registerOutParameter(1, java.sql.Types.FLOAT);
// stmt.registerOutParameter(2, java.sql.Types.CHAR);
stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(2, in_price);
stmt.executeUpdate();
// 取的結果集的方式一:
rs = ((OracleCallableStatement) stmt).getCursor(1);
// 取的結果集的方式二:
// rs = (ResultSet) stmt.getObject(1);
String ric;
String price;
String updated;
while (rs.next()) {
ric = rs.getString(1);
price = rs.getString(2);
updated = rs.getString(3);
System.out.println("ric:" + ric + ";-- price:" + price + "; --"
+ updated + "; ");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("close ");
}
}
public static void main(String args[])// 自己替換[]
{
new JDBCoracle10G_INVOKEFUNCTION();
}
}
⑷ 怎麼實現用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();
}
}
}
}
}
⑸ java怎麼調用oracle的過程
參考下面代碼
java下實現調用oracle的存儲過程和函數
在oracle下創建一個test的賬戶,然後按一下步驟執行:
1.創建表:STOCK_PRICES
--創建表格
CREATE TABLE STOCK_PRICES(
RIC VARCHAR(6) PRIMARY KEY,
PRICE NUMBER(7,2),
UPDATED DATE );
2.插入測試數據:
--插入數據
INSERT INTO stock_prices values('1111',1.0,SYSDATE);
INSERT INTO stock_prices values('1112',2.0,SYSDATE);
INSERT INTO stock_prices values('1113',3.0,SYSDATE);
INSERT INTO stock_prices values('1114',4.0,SYSDATE);
3.建立一個返回遊標: PKG_PUB_UTILS
--建立一個返回遊標
CREATE OR REPLACE PACKAGE PKG_PUB_UTILS IS
--動態游標
TYPE REFCURSOR IS REF CURSOR;
END PKG_PUB_UTILS;
4.創建和存儲過程:P_GET_PRICE
--創建存儲過程
CREATE OR REPLACE PROCEDURE P_GET_PRICE
(
AN_O_RET_CODE OUT NUMBER,
AC_O_RET_MSG OUT VARCHAR2,
CUR_RET OUT PKG_PUB_UTILS.REFCURSOR,
AN_I_PRICE IN NUMBER
)
IS
BEGIN
AN_O_RET_CODE := 0;
AC_O_RET_MSG := '操作成功';
OPEN CUR_RET FOR
SELECT * FROM STOCK_PRICES WHERE PRICE<AN_I_PRICE;
EXCEPTION
WHEN OTHERS THEN
AN_O_RET_CODE := -1;
AC_O_RET_MSG := '錯誤代碼:' || SQLCODE || CHR(13) || '錯誤信息:' || SQLERRM;
END P_GET_PRICE;
5.創建函數:
--創建函數:F_GET_PRICE
CREATE OR REPLACE FUNCTION F_GET_PRICE(v_price IN NUMBER)
RETURN PKG_PUB_UTILS.REFCURSOR
AS
stock_cursor PKG_PUB_UTILS.REFCURSOR;
BEGIN
OPEN stock_cursor FOR
SELECT * FROM stock_prices WHERE price < v_price;
RETURN stock_cursor;
END;
6.JAVA調用存儲過程返回結果集
代碼示例:JDBCoracle10G_INVOKEPROCEDURE.java
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
/* 本例是通過調用oracle的存儲過程來返回結果集:
* oracle 9i、10G 的jdbc由1個jar包組成:classes12.zip
*/
public class JDBCoracle10G_INVOKEPROCEDURE {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
CallableStatement stmt = null;
String driver;
String url;
String user;
String pwd;
String sql;
String in_price;
public JDBCoracle10G_INVOKEPROCEDURE()
{
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:ORCL";
// oracle 用戶
user = "test";
// oracle 密碼
pwd = "test";
init();
// mysid:必須為要連接機器的sid名稱,否則會包以下錯:
// java.sql.SQLException: Io 異常: Connection
// refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
// 參考連接方式:
// Class.forName( "oracle.jdbc.driver.OracleDriver" );
// cn = DriverManager.getConnection(
// "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
}
public void init() {
System.out.println("oracle jdbc test");
try {
Class.forName(driver);
System.out.println("driver is ok");
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("conection is ok");
statement = conn.createStatement();
// conn.setAutoCommit(false);
// 輸入參數
in_price = "3.0";
// 調用函數
stmt = conn.prepareCall("call P_GET_PRICE(?,?,?,?)");
stmt.registerOutParameter(1, java.sql.Types.FLOAT);
stmt.registerOutParameter(2, java.sql.Types.CHAR);
stmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(4, in_price);
stmt.executeUpdate();
int retCode = stmt.getInt(1);
String retMsg = stmt.getString(2);
if (retCode == -1) { // 如果出錯時,返回錯誤信息
System.out.println("報錯!");
} else {
// 取的結果集的方式一:
rs = ((OracleCallableStatement) stmt).getCursor(3);
// 取的結果集的方式二:
// rs = (ResultSet) stmt.getObject(3);
String ric;
String price;
String updated;
// 對結果進行輸出
while (rs.next()) {
ric = rs.getString(1);
price = rs.getString(2);
updated = rs.getString(3);
System.out.println("ric:" + ric + ";-- price:" + price
+ "; --" + updated + "; ");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("close ");
}
}
public static void main(String args[])// 自己替換[]
{
new JDBCoracle10G_INVOKEPROCEDURE();
}
}
7.開發JAVA調用函數返回結果集
代碼示例:JDBCoracle10G_INVOKEFUNCTION.java
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
/*
/* 本例是通過調用oracle的函數來返回結果集:
* oracle 9i、10G 的jdbc由1個jar包組成:classes12.zip
*/
public class JDBCoracle10G_INVOKEFUNCTION {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
CallableStatement stmt = null;
String driver;
String url;
String user;
String pwd;
String sql;
String in_price;
public JDBCoracle10G_INVOKEFUNCTION()
{
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:ORCL";
// oracle 用戶
user = "test";
// oracle 密碼
pwd = "test";
init();
// mysid:必須為要連接機器的sid名稱,否則會包以下錯:
// java.sql.SQLException: Io 異常: Connection
// refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
// 參考連接方式:
// Class.forName( "oracle.jdbc.driver.OracleDriver" );
// cn = DriverManager.getConnection(
// "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
}
public void init() {
System.out.println("oracle jdbc test");
try {
Class.forName(driver);
System.out.println("driver is ok");
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("conection is ok");
statement = conn.createStatement();
// conn.setAutoCommit(false);
// 輸入參數
in_price = "5.0";
// 調用函數
stmt = conn.prepareCall("{? = call F_GET_PRICE(?)}");
// stmt.registerOutParameter(1, java.sql.Types.FLOAT);
// stmt.registerOutParameter(2, java.sql.Types.CHAR);
stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(2, in_price);
stmt.executeUpdate();
// 取的結果集的方式一:
rs = ((OracleCallableStatement) stmt).getCursor(1);
// 取的結果集的方式二:
// rs = (ResultSet) stmt.getObject(1);
String ric;
String price;
String updated;
while (rs.next()) {
ric = rs.getString(1);
price = rs.getString(2);
updated = rs.getString(3);
System.out.println("ric:" + ric + ";-- price:" + price + "; --"
+ updated + "; ");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("close ");
}
}
public static void main(String args[])// 自己替換[]
{
new JDBCoracle10G_INVOKEFUNCTION();
}
}
⑹ 怎麼用java連接oracle資料庫,需要詳細解釋
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();
}
}
⑺ 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);
⑻ 如何用java連接oracle資料庫
public class OracleJdbcTest
{
String driverClass = "oracle.jdbc.driver.OracleDriver";
Connection con;
public void init(FileInputStream fs) throws Exception
{
Properties props = new Properties();
props.load(fs);
String url = props.getProperty("db.url");
String userName = props.getProperty("db.user");
String password = props.getProperty("db.password");
Class.forName(driverClass);
con=DriverManager.getConnection(url, userName, password);
}
public void fetch() throws SQLException, IOException
{
PreparedStatement ps = con.prepareStatement("select SYSDATE from al");
ResultSet rs = ps.executeQuery();
while (rs.next())
{
// do the thing you do
}
rs.close();
ps.close();
}
public static void main(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest();
test.init();
test.fetch();
}
}
[代碼] db.properties
db.url = jdbc:oracle:thin:@::
db.user = sa
db.password =