List of usage examples for java.sql ResultSet getString
String getString(String columnLabel) throws SQLException;
ResultSet
object as a String
in the Java programming language. From source file:MainClass.java
public static void main(String[] arguments) { String data = "jdbc:odbc:YourSettings"; try {//from w ww . jav a 2 s. co m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection(data, "", ""); Statement st = conn.createStatement(); ResultSet rec = st .executeQuery("SELECT * FROM Coal WHERE (Country='" + arguments[0] + "') ORDER BY Year"); while (rec.next()) { System.out.println(rec.getString(1) + "\t" + rec.getString(2) + "\t\t" + rec.getString(3) + "\t" + rec.getString(4)); } st.close(); } catch (Exception e) { System.out.println("Error: " + e.toString() + e.getMessage()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from ww w.jav a2s.c o m String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); // Create a scrollable result set Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); while (resultSet.next()) { // Get data at cursor String s = resultSet.getString(1); } while (resultSet.previous()) { // Get data at cursor String s = resultSet.getString(1); } // Move cursor to the first row resultSet.first(); // Move cursor to the last row resultSet.last(); // Move cursor to the end, after the last row resultSet.afterLast(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); st.executeUpdate("create table survey (id int,name varchar(30));"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,null)"); st.executeUpdate("insert into survey (id,name ) values (3,'Tom')"); ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet scroll from top while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); }// w w w . j a va 2s . c o m System.out.println("---------"); // scroll from the bottom rs.afterLast(); while (rs.previous()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); ResultSet rs = null; DatabaseMetaData meta = conn.getMetaData(); rs = meta.getTableTypes();/*from w ww.j a v a2 s . co m*/ while (rs.next()) { String tableType = rs.getString(1); System.out.println("tableType=" + tableType); } st.close(); conn.close(); }
From source file:JavaDBDemo.java
public static void main(String[] args) { String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String connectionURL = "jdbc:derby:myDatabase;create=true"; String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)"; try {// ww w . j ava 2 s . c om Class.forName(driver); } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(connectionURL); Statement stmt = conn.createStatement(); stmt.executeUpdate(createString); PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)"); psInsert.setString(1, args[0]); psInsert.setString(2, args[1]); psInsert.executeUpdate(); Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery("select * from Employee"); int num = 0; while (rs.next()) { System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2)); } rs.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar, PRIMARY KEY (id) );"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); DatabaseMetaData meta = conn.getMetaData(); ResultSet rs = meta.getPrimaryKeys(null, null, "survey"); java.util.List list = new java.util.ArrayList(); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); System.out.println("getPrimaryKeys(): columnName=" + columnName); }// w w w .j a va2s. c om st.close(); conn.close(); }
From source file:TestThinDSApp.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { // These settings are typically configured in JNDI // so they a implementation specific OracleDataSource ds = new OracleDataSource(); ds.setDriverType("thin"); ds.setServerName("dssw2k01"); ds.setPortNumber(1521);/*from ww w. j a va2 s . c o m*/ ds.setDatabaseName("orcl"); // sid ds.setUser("scott"); ds.setPassword("tiger"); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "select 'Hello Thin driver data source tester '||" + "initcap(USER)||'!' result from dual"); if (rset.next()) System.out.println(rset.getString(1)); rset.close(); stmt.close(); conn.close(); }
From source file:TestSSL.java
public static void main(String[] argv) throws Exception { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Properties prop = new Properties(); prop.setProperty("user", "scott"); prop.setProperty("password", "tiger"); // THIS DOES NOT WORK YET prop.setProperty("oracle.net.ssl_cipher_suites", "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha)"); prop.setProperty("oracle.net.ssl_client_authentication", "false"); prop.setProperty("oracle.net.ssl_version", "3.0"); prop.setProperty("oracle.net.encryption_client", "REJECTED"); prop.setProperty("oracle.net.crypto_checksum_client", "REJECTED"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCPS)(HOST = dssw2k01)(PORT = 2484))) (CONNECT_DATA = (SERVICE_NAME = DSSW2K01)))", prop);//from w w w. j a va 2s . c o m Statement stmt = conn.createStatement(); ResultSet rset = stmt .executeQuery("select 'Hello Thin driver SSL " + "tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);//from w ww. j a va 2 s.com String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); // Create a scrollable result set Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table"); // Move cursor forward while (resultSet.next()) { // Get data at cursor String s = resultSet.getString(1); } // Move cursor backward while (resultSet.previous()) { // Get data at cursor String s = resultSet.getString(1); } }
From source file:TestDataEncryptionIntegrity.java
public static void main(String[] argv) throws Exception { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Properties prop = new Properties(); prop.setProperty("user", "scott"); prop.setProperty("password", "tiger"); prop.setProperty("oracle.net.encryption_client", "REQUIRED"); prop.setProperty("oracle.net.encryption_types_client", "( RC4_40 )"); prop.setProperty("oracle.net.crypto_checksum_client", "REQUIRED"); prop.setProperty("oracle.net.crypto_checksum_types_client", "( MD5 )"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", prop); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery( "select 'Hello Thin driver Encryption & Integrity " + "tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();/* w ww . j a v a 2 s . c om*/ stmt.close(); conn.close(); }