List of usage examples for java.sql Statement executeQuery
ResultSet executeQuery(String sql) throws SQLException;
ResultSet
object. From source file:PrintResultSet.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee"); ResultSetMetaData md = rs.getMetaData(); int nColumns = md.getColumnCount(); for (int i = 1; i <= nColumns; i++) { System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t")); }//from w ww . j av a 2 s . c o m while (rs.next()) { for (int i = 1; i <= nColumns; i++) { System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t")); } } }
From source file:MainClass.java
public static void main(String[] arguments) { String data = "jdbc:odbc:YourSettings"; try {// w ww . j a v a 2 s .c om 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:TestThinNet8App.java
public static void main(String args[]) throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@(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 Thin driver tester '||USER||'!' result from dual"); while (rset.next()) System.out.println(rset.getString(1)); rset.close();//from w w w. j a v a2 s .c o m stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection con = null;//from w w w . j a v a 2 s .c o m Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver"); con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/Keyspace"); String query = "SELECT * FROM column_family where gender=f "; Statement stmt = con.createStatement(); ResultSet result = stmt.executeQuery(query); while (result.next()) { System.out.println(result.getString("user_name")); System.out.println(result.getString("gender")); System.out.println(result.getString("password")); } con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { System.setProperty("oracle.net.tns_admin", "C:/app/product/11.2.0/client_1/NETWORK/ADMIN"); String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES"; Class.forName("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection(dbURL, "your_user_name", "your_password"); System.out.println("Connection established"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual"); if (rs.next()) { System.out.println(rs.getString(1)); }/*w ww .j av a 2 s . c o m*/ stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn = null;/*from w ww . j a v a 2s . co m*/ Properties info = new Properties(); // info.put("proxy_type", "4"); // SSL Tunneling info.put("proxy_host", "[proxy host]"); info.put("proxy_port", "[proxy port]"); info.put("proxy_user", "[proxy user]"); info.put("proxy_password", "[proxy password]"); info.put("user", "[db user]"); info.put("password", "[db pass word]"); conn = DriverManager.getConnection("jdbc:mysql://[db host]/", info); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("Select NOW()"); rs.next(); System.out.println("Data- " + rs.getString(1)); rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String db_file_name_prefix = "c:\\mydbdir\\mydb"; Connection con = null;/*from w w w. j a v a 2 s . c o m*/ Class.forName("org.hsqldb.jdbcDriver"); con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames "sa", // username ""); // password Statement statement = con.createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM \"User\""); while (rs.next()) { System.out.print("ID: " + rs.getString("ID")); System.out.print(" first name: " + rs.getString("firstname")); System.out.println(" last name: " + rs.getString("lastname")); } statement.close(); con.close(); }
From source file:TestOCIApp.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:@dssnt01", "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 w w w.j a v a2s .c o m*/ stmt.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 v a 2s.co 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:ChainedExceptionDemo.java
public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String connectionURL = "jdbc:mysql://127.0.0.1:3306/sample"; try {//from w ww . j ava2 s . c om Class.forName(driver); } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection(connectionURL); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM NONEXISTINGTABLE"); rs.next(); rs.close(); } catch (SQLException sx) { for (Throwable e : sx) { System.err.println("Error encountered: " + e); } } }