Java Connection Oracle SQL
今天測試了ORACLE連線,以下是連線範例(Oracle 是11g):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleTest {
String dbUrl = "jdbc:oracle:thin:@IP:SID";
String theUser = "USERID";
String thePw = "PASSWORD";
Connection c = null;
Statement conn;
ResultSet rs = null;
public OracleTest() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
c = DriverManager.getConnection(dbUrl, theUser, thePw);
conn = c.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean executeUpdate(String sql) {
try {
conn.executeUpdate(sql);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
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();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ResultSet rs;
OracleTest conn = new OracleTest();
rs = conn.executeQuery("select * from dba_users");
try {
while (rs.next()) {
System.out.println("username:" + rs.getString("username")+"-- user_id:" + rs.getString("user_id"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleTest {
String dbUrl = "jdbc:oracle:thin:@IP:SID";
String theUser = "USERID";
String thePw = "PASSWORD";
Connection c = null;
Statement conn;
ResultSet rs = null;
public OracleTest() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
c = DriverManager.getConnection(dbUrl, theUser, thePw);
conn = c.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean executeUpdate(String sql) {
try {
conn.executeUpdate(sql);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
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();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ResultSet rs;
OracleTest conn = new OracleTest();
rs = conn.executeQuery("select * from dba_users");
try {
while (rs.next()) {
System.out.println("username:" + rs.getString("username")+"-- user_id:" + rs.getString("user_id"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
留言
張貼留言