package api;
import java.sql.*;
public class TestOracle {
public static void main(String args[]) {
Connection connection;
Statement stmt;
ResultSet rs;
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
// Create a connection to the database (get connection info from $ORACLE_HOME\NETWORK\ADMIN\tnsnames.ora)
String serverName = "VTT_VE_HUYPV";
String portNumber = "1521";
String sid = "orcl";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber
+ ":" + sid;
String username = "portal";
String password = "abc";
connection = DriverManager.getConnection(url, username, password);
stmt = connection.createStatement();
int offset = 0;
int limit = 1000;
String sql = "SELECT * FROM (SELECT rownum rn, a.* from HR.COUNTRIES a) WHERE rn > "+String.valueOf(offset)+" AND rn <= "+String.valueOf(offset + limit);
// Create a scrollable ResultSet.
stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
// Point to the last row in resultset.
rs.last();
// Get the row position which is also the number of rows in the
// ResultSet.
int rowcount = rs.getRow();
System.out.println("Total rows for the query: " + rowcount);
// Reposition at the beginning of the ResultSet to take up rs.next()
// call.
rs.beforeFirst();
while (rs.next()) {
System.out.println(rs.getString(2) + " = " + rs.getString(3));
}
} catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.println("404 DB Driver");
} catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
}
}
}
Title:
Java connect to Oracle, select data
Description:
package api; import java.sql.*; public class TestOracle { public static void main(String args[]) { Connection connection; ...
...
Rating:
4