java操作oracle
用eclipse
首先在oracle文件下找到jdbc文件,裡面的lib文件下有個class12.zip
在eclipse里新建個項目,建好後在左邊右擊那個項目圖標,選擇屬性(最下面的),裡面有個java build path,點進去,找到add external jars按鍵,點一下,把那個class12.zip載入進來,然後可以開始寫代碼了。
我的代碼如下:
import java.sql.*;
public class connect {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
String url="jdbc:oracle:thin:@伺服器ip地址:埠號:資料庫名字";
String user="用戶名";
String password="密碼";
String str="INSERT INTO ZZZ_2 VALUES('041110018','JHDK')";
con = java.sql.DriverManager.getConnection(url,user,password);
// 創建狀態
stmt = con.createStatement();
// 執行SQL語句,返回結果集
//int rowcount = stmt.executeUpdate(str);
int j = stmt.executeUpdate("update ZZZ_2 set NAME='dbt' where ID=21");
int k = stmt.executeUpdate("delete from ZZZ_2 where ID=41110020");
rs = stmt.executeQuery("SELECT * FROM ZZZ_2");
// 對結果集進行處理
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("NAME");
//Integer age = rs.getObject("age") == null ? null : rs.getInt("age");
System.out.println(id + ": " + name);
} }catch(SQLException e){
e.printStackTrace();}
// 釋放資源
finally{
try{
rs.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
stmt.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
con.close();
}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
注意,上面的ip地址什麼的要填對,ZZZ_2是我隨便建的表,你改成自己的表名就行了
『貳』 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
一個最簡單連接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資料庫,需要詳細解釋
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資料庫
這個是完全沒有問題的。
先期准備:
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();
}
}
}
}
}