當前位置:首頁 » 數據倉庫 » java連接oracle資料庫測試
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java連接oracle資料庫測試

發布時間: 2023-06-04 08:46:58

1. java 連接oracle

給你一個能看懂的。
記得給兄我多加點分。謝謝了啊。
# 1、Oracle8/8i/9i資料庫(thin模式)
# 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);
#
# 2、DB2資料庫
# Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
# String url="jdbc:db2://localhost:5000/sample";
# //sample為你的資料庫名
# String user="admin";
# String password="";
# Connection conn= DriverManager.getConnection(url,user,password);
#
# 3、sql Server7.0/2000資料庫
# Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
# String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
# //mydb為資料庫
# String user="sa";
# String password="";
# Connection conn= DriverManager.getConnection(url,user,password);
#
# 4、Sybase資料庫
# Class.forName("com.sybase.jdbc.SybDriver").newInstance();
# String url =" jdbc:sybase:Tds:localhost:5007/myDB";
# //myDB為你的資料庫名
# Properties sysProps = System.getProperties();
# SysProps.put("user","運派userid");
# SysProps.put("password","user_password");
# Connection conn= DriverManager.getConnection(url, SysProps);
#
# 5、Informix資料庫
# Class.forName("com.informix.jdbc.IfxDriver"孝大).newInstance(); String url =
# "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
# user=testuser;password=testpassword";
# //myDB為資料庫名
# Connection conn= DriverManager.getConnection(url);
#
# 6、MySQL數據旁慎賀庫
# Class.forName("org.gjt.mm.mysql.Driver").newInstance();
# String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
# //myDB為資料庫名
# Connection conn= DriverManager.getConnection(url);
#
# 7、PostgreSQL資料庫
# Class.forName("org.postgresql.Driver").newInstance();
# String url ="jdbc:postgresql://localhost/myDB"
# //myDB為資料庫名
# String user="myuser";
# String password="mypassword";
# Connection conn= DriverManager.getConnection(url,user,password);

2. 如何用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();
}
}
}

3. 怎麼實現用java訪問oracle資料庫

實現用java訪問oracle資料庫:

准備工作:

1.下載驅動,打開瀏覽器,搜索oracle資料庫驅動,進入下載頁面下載即可;

2.將驅動導入到項目中。

用java訪問oracle資料庫代碼實現:



try{
Class.forName("oracle.jdbc.driver.OracleDriver");//載入驅動
//與資料庫建立連接
try{
Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";
Stringuser="scott";//用戶名
Stringpassword="tiger";//密碼
conn=DriverManager.getConnection(url,username,password);
}catch(SQLExceptione){
e.printStackTrace();
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}

4. 如何用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 =

5. 求一個連接oracle資料庫的java類和一個測試是否連接成功的JAVA類

import java.sql.*;
public class getMetaData {
public static void main(String[] args) {

String driverClassName = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@192.101.101.101:1521:orcl";
String password = "test";
String user= "test";

try {
Class.forName(driverClassName);
} catch (ClassNotFoundException ex) {
System.out.println("載入錯誤!");
}
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("連接成功");

String sql = "select id_ tid,name_ tname1 ,memo remark from tc";
PreparedStatement ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=ps.executeQuery();
ResultSetMetaData md = ps.getMetaData(); //ps.getMetaData();//
for (int i = 1; i <= md.getColumnCount(); i++) {
System.out.println("name:"+md.getColumnName(i)+" label:"+md.getColumnLabel(i));
}
sql = "select max(id_) tid,name_ tname1 ,memo remark from tc group by name_,memo";
ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=ps.executeQuery();
md = rs.getMetaData(); //ps.getMetaData();//
for (int i = 1; i <= md.getColumnCount(); i++) {
System.out.println("name:"+md.getColumnName(i)+" label:"+md.getColumnLabel(i));
}

} catch (SQLException ex1) {
System.out.println(ex1);
System.out.println("失敗");
}
}
}

6. JAVA JDBC連接Oracle資料庫 進行壓力測試

你好,你的UserDao第46行已經報空指針了,還有Connection reset是估計你的連接池資源不夠達到上限了!

7. java與oracle資料庫的連接

程序運行時是不需要myeclipse與資料庫作連接斗行仿得,程序連接orcle的代帶晌碼最下樓得哥們都貼出來了,借來用用.

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl為資料庫的SID

Stringuser="test";

Stringpassword="test";

Connectionconn=DriverManager.getConnection(url,user,password);

myeclipse得資料庫連接相當於資料庫得客戶端,用來直接操作或查看資料庫使用得,當然,也可以產生一些orm映射代碼,非常方便.

首先選擇myeclipsedatabaseexplorer打開資料庫窗口,參看圖片一

然空纖後配置到oracle得連接,我暈,只能上傳一個圖片.

哥們,郵件給我,我發給你[email protected]

8. 怎麼用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();
}

}