public class Demo_jdbc {
private static final String URL = “jdbc:oracle:thin:@//localhost:1521/orcl”;
private static final String USERNAME = “用户名”;
private static final String PASSWORLD = “用户密码”;
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 加载驱动类
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
// 获取连接对象
connection = DriverManager.getConnection(URL, USERNAME, PASSWORLD);
// 获取Statement对象
statement = connection.createStatement();
String SQL = "SQL语句";
resultSet = statement.executeQuery(SQL);
while (resultSet.next()) {
//getInt()和getString()方法的索引值从1开始,表示第一个字段。
int anInt = resultSet.getInt(1);
Date date = resultSet.getDate("creationdate");
String title = resultSet.getString("title");
System.out.println(anInt + "=============" + date + "============" + title);
}
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}