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 =
㈡ 怎麼用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
一個最簡單連接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資料庫
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();
}