Last active
December 28, 2015 04:59
-
-
Save KimiyukiYamauchi/7446177 to your computer and use it in GitHub Desktop.
javaからOracleに接続
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class DataAccess { | |
public static void main(String[] args) { | |
DataAccess dataAccess =new DataAccess(); | |
try { | |
dataAccess.selectOracle(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void selectOracle() throws Exception{ | |
/* ユーザ名 */ | |
String user = "s11011"; | |
/* パスワード */ | |
String pass = "password"; | |
/* サーバ名 */ | |
String servername = "172.16.40.4"; | |
/* SID */ | |
String sid = "db11"; | |
Connection conn = null; | |
Statement stmt = null; | |
ResultSet rset = null; | |
try { | |
/* ドライバクラスのロード */ | |
Class.forName ("oracle.jdbc.OracleDriver"); | |
/* Connectionの作成 */ | |
conn = DriverManager.getConnection | |
("jdbc:oracle:thin:@" + servername + ":1521:" + sid,user,pass); | |
/* Statementの作成 */ | |
stmt = conn.createStatement(); | |
/* Resultsetの作成 */ | |
rset = stmt.executeQuery("select ENAME from EMPLOYEES"); | |
//rset = stmt.executeQuery (query); | |
/* 取得したデータを表示します。 */ | |
while (rset.next()) { | |
System.out.println(rset.getString(1)); | |
} | |
} catch (ClassNotFoundException e) { | |
throw e; | |
} catch (SQLException e) { | |
throw e; | |
} catch ( Exception e){ | |
throw e; | |
} | |
finally{ | |
/* クローズ処理 */ | |
if(conn != null){ | |
conn.close(); | |
conn = null; | |
} | |
if(stmt != null){ | |
stmt.close(); | |
stmt = null; | |
} | |
if(rset != null){ | |
rset.close(); | |
rset = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以下からjdbcドライバをダウンロードして、クラスパスを通す必要あり。
http://www.oracle.com/technetwork/jp/database/enterprise-edition/jdbc-111060-097832-ja.html