Java串連oracle資料庫——-jdbc的thin方式

來源:互聯網
上載者:User

此方法在使用時需要將oracle的jar包加到classpath變數中,此包可以在oralce用戶端程式的$ORACLE_HOME/jdbc/lib/classes12.jar找到。

import java.sql.*;

public class jdbcthin {
//dbUrl資料庫連接串資訊,其中“1521”為連接埠,“ora9”為sid
String dbUrl = "jdbc:oracle:thin:@10.10.20.15:1521:ora9";
//theUser為資料庫使用者名稱
String theUser = "sman";
//thePw為資料庫密碼
String thePw = "sman";
//幾個資料庫變數
Connection c = null;
Statement conn;
ResultSet rs = null;

//初始化串連
public jdbcthin() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//與url指定的資料來源建立串連
c = DriverManager.getConnection(dbUrl, theUser, thePw);
//採用Statement進行查詢
conn = c.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}

//執行查詢
public ResultSet executeQuery(String sql) {
rs = null;
try {
rs = conn.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}

public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
ResultSet newrs;
jdbcthin newjdbc = new jdbcthin();
newrs = newjdbc.executeQuery("select * from eventtype");
try {
while (newrs.next()) {
System.out.print(newrs.getString("event_type"));
System.out.println(":"+newrs.getString("content"));
}
} catch (Exception e) {
e.printStackTrace();
}

newjdbc.close();
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.