java與oracle連接
Ⅰ java與oracle連接
192.168.3.9是資料庫ip地址,1521資料庫的訪問埠
Ⅱ 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資料庫
8.Oracle8/8i/9i資料庫(thin模式)
//import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();
1.提取單條記錄
//import java.sql.*;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
stmt.executeUpdate(%%4);
%%6=stmt.executeQuery(%%5);
%%7
}catch(Exception e){
e.printStackTrace();
}finally{
try {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.單值比較
//import java.sql.*;
ResultSet rs;
String strurl="jdbc:odbc:%%1";
Connection con=NULL;
Statement stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(%%2);
rs.next();
int %%3=rs.getInt("%%3");
if(%%3==%%4)
{
%%5
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(rs!=null)
{
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3.顯示表格
//import java.awt.*;
//import javax.swing.*;
//import java.sql.*;
//import javax.swing.table.*;
String[] colHeads=%%4;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
%%6=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
%%6.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
%%6=stmt.executeQuery("SELECT * from "+%%5);
while(%%6.next()){
data[i]=new Object[iCount];
data[i][0]=%%6.getString("au_fname");
data[i][1]=%%6.getString("Phone");
data[i][2]=%%6.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(con!=null){
try {
%%6.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
7.執行命令
//import java.sql.*;
Connection conn;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setInt(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
pst.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);
Ⅳ oracle與Java連接方法
import java.sql.Connection; import java.sql.DriverManager; import java.這樣連接基本上沒有什麼錯誤。你可以試試,很可能就是你的oracle服務沒有啟動
Ⅵ java連接oracle
應該是這里錯了
ResultSet ji1 = (ResultSet) cs.getObject(6);
改成
ResultSet ji1 = (ResultSet) cs.getObject(5);
設值是1開始,取值從0開始。
Ⅶ java操控oracle建立連接
你這樣發出來大家是無法給出判斷是哪裡錯誤的。最起碼得把錯誤信息提示給貼出來吧。conn是一個connection的連接對象。不管是ORacle還是其他的資料庫總是要拿這個連接對象。因為,它們就是jdbc的東西。通過驅動管理對象DriverManager獲得一個連接。所以,這里url,user,password都要寫對!如果想要得到更好的解釋,那就把錯誤提示信息貼出來吧
Ⅷ 怎麼用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();
}
}