List of usage examples for java.sql Statement executeQuery
ResultSet executeQuery(String sql) throws SQLException;
ResultSet
object. 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();/*from w ww . ja v a2s . c o m*/ stmt.close(); conn.close(); }
From source file:TestOCINet8App.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // or you can use: // DriverManager.registerDriver( // new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@(DESCRIPTION = " + "(ADDRESS_LIST = (ADDRESS = " + "(PROTOCOL = TCP)(HOST = dssw2k01)(PORT = 1521)))" + "(CONNECT_DATA = (SERVICE_NAME = dssw2k01)))", "scott", "tiger"); Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery("select 'Hello OCI driver tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();//from www .ja v a 2 s . c om stmt.close(); conn.close(); }
From source file:Example.java
public static void main(String args[]) { try {// w w w. j a v a 2s .co m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:inventory", "", ""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM inventory ORDER BY price"); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); int rowCount = 1; while (rs.next()) { for (int i = 1; i <= numberOfColumns; i++) { System.out.print(rs.getString(i) + " "); } rowCount++; } stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/* w w w . j a v a2 s. 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); Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM my_table"); // Get the number of rows from the result set resultSet.next(); int rowcount = resultSet.getInt(1); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/* ww w . j a v a 2s. c o m*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); if (metadata.isAutoIncrement(1)) { System.out.println("Column ID is an auto-increment column"); } }
From source file:ResultSetExample.java
public static void main(String args[]) { try {/*from ww w. j av a 2 s . c o m*/ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT SupplierName,ProductName, Price " + "FROM ProductSuppliersView WHERE CategoryName LIKE '%BEVERAGES%' "); while (rs.next()) { String supplier = rs.getString("SupplierName"); String product = rs.getString("ProductName"); int price = rs.getInt("Price"); System.out.println(supplier + " sells " + product + " for $" + price); } stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:MainClass.java
public static void main(String[] args) { try {/*from w ww . j a v a2 s. com*/ Class.forName("COM.cloudscape.core.JDBCDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER"); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM tableName"); while (rs.next()) { int id = rs.getInt("GAMEID"); Document manual = (Document) rs.getObject("MANUAL"); XMLSerializer serialize = new XMLSerializer(System.out, null); serialize.serialize(manual); } } catch (Throwable e) { System.out.println("exception thrown"); System.out.println(e); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("DESCRIBE myTable"); ResultSetMetaData md = rs.getMetaData(); int col = md.getColumnCount(); for (int i = 1; i <= col; i++) { String col_name = md.getColumnName(i); System.out.println(col_name); }/* www .j a va 2 s. co m*/ DatabaseMetaData dbm = con.getMetaData(); ResultSet rs1 = dbm.getColumns(null, "%", "myTable", "%"); while (rs1.next()) { String col_name = rs1.getString("COLUMN_NAME"); String data_type = rs1.getString("TYPE_NAME"); int data_size = rs1.getInt("COLUMN_SIZE"); int nullable = rs1.getInt("NULLABLE"); System.out.println(col_name + " " + data_type + "(" + data_size + ")"); if (nullable == 1) { System.out.println("YES"); } else { System.out.println("NO"); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/*from ww w .j a va 2 s . c o m*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT id, username FROM users"); ResultSetMetaData metadata = resultSet.getMetaData(); int nullability = metadata.isNullable(1); if (nullability == ResultSetMetaData.columnNullable) { System.out.println("Columns ID can have a null value"); } else if (nullability == ResultSetMetaData.columnNoNulls) { System.out.println("Columns ID does not allowed to have a null value"); } else if (nullability == ResultSetMetaData.columnNullableUnknown) { System.out.println("Nullability unknown"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName);/* w w w. ja v a 2 s. 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); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT col_blob FROM mysql_all_table"); if (rs.next()) { Blob blob = rs.getBlob("col_blob"); long blobLength = blob.length(); int pos = 1; // position is 1-based int len = 10; byte[] bytes = blob.getBytes(pos, len); InputStream is = blob.getBinaryStream(); int b = is.read(); } }