List of usage examples for java.sql ResultSet getInt
int getInt(String columnLabel) throws SQLException;
ResultSet
object as an int
in the Java programming language. From source file:AutoGenKeys.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con = null;/*from w ww. j a va2 s .co m*/ PreparedStatement pstmt; String insert = "INSERT INTO COFFEES VALUES ('HYPER_BLEND', " + "101, 10.99, 0, 0)"; String update = "UPDATE COFFEES SET PRICE = ? WHERE KEY = ?"; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); pstmt = con.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS); pstmt.executeUpdate(); ResultSet keys = pstmt.getGeneratedKeys(); int count = 0; keys.next(); int key = keys.getInt(1); pstmt = con.prepareStatement(update); pstmt.setFloat(1, 11.99f); pstmt.setInt(2, key); pstmt.executeUpdate(); keys.close(); pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws SQLException { // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@yourDB", "scott", "tiger"); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.setFetchSize(1);/*from w w w .ja v a2s . co m*/ ResultSet rset = stmt.executeQuery("select EMPNO, ENAME, SAL from EMP"); showProperty(rset); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3)); } doSomeChanges(conn); // Place the cursor before the first row rset.beforeFirst(); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3)); } rset.close(); stmt.close(); cleanup(conn); conn.close(); }
From source file:id.aas.apps.mvc.view.pieChart.java
public static void main(String[] args) throws SQLException { // pieChart p = new pieChart(); // p.setVisible(true); // p.run(); EventQueue.invokeLater(new Runnable() { @Override/*from www .ja v a2s .co m*/ public void run() { try { int nilaiKiloan = 0; int nilaiSatuan = 0; pieChart frame = new pieChart(); frame.setVisible(true); String queryKiloan = "SELECT COUNT(*) FROM laundry WHERE jenis_layanan='Laundry Kiloan'"; String querySatuan = "SELECT COUNT(*) FROM laundry WHERE jenis_layanan='Laundry Satuan'"; ConnectionDB.InstanceDB.openConnection(); ResultSet rs = ConnectionDB.InstanceDB.RunSelectQuery(queryKiloan); while (rs.next()) { nilaiKiloan = rs.getInt(1); } System.out.println(nilaiKiloan); ResultSet rs1 = ConnectionDB.InstanceDB.RunSelectQuery(querySatuan); while (rs1.next()) { nilaiSatuan = rs1.getInt(1); } System.out.println(nilaiSatuan); DefaultPieDataset pieDataset = new DefaultPieDataset(); pieDataset.setValue("Laundry Kiloan", nilaiKiloan); pieDataset.setValue("Laundry Satuan", nilaiSatuan); JFreeChart chart = ChartFactory.createPieChart3D("Perbandingan Pengguna Layanan Laundry", pieDataset, true, true, false); ChartPanel cPanel = new ChartPanel(chart); frame.setContentPane(cPanel); } catch (Exception e) { e.printStackTrace(); } } }); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); 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')"); st = conn.createStatement();//from w w w . ja v a 2 s.c o m ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs = st.executeQuery("SELECT COUNT(*) FROM survey"); // get the number of rows from the result set rs.next(); int rowCount = rs.getInt(1); System.out.println(rowCount); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 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')"); st = conn.createStatement();/* ww w . j av a 2 s . com*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs = st.executeQuery("SELECT COUNT(*) FROM survey"); // get the number of rows from the result set rs.next(); int rowCount = rs.getInt(1); System.out.println(rowCount); rs.close(); st.close(); conn.close(); }
From source file:Logging.java
public static void main(String args[]) throws Exception { FileOutputStream errors = new FileOutputStream("StdErr.txt", true); PrintStream stderr = new PrintStream(errors); PrintWriter errLog = new PrintWriter(errors, true); System.setErr(stderr);/* w w w. ja v a 2 s. com*/ String query = "SELECT Name,Description,Qty,Cost,Sell_Price FROM Stock"; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String name = rs.getString("Name"); String desc = rs.getString("Description"); int qty = rs.getInt("Qty"); float cost = rs.getFloat("Cost"); } } catch (ClassNotFoundException e) { e.printStackTrace(errLog); } catch (SQLException e) { System.err.println((new GregorianCalendar()).getTime()); System.err.println("Thread: " + Thread.currentThread()); System.err.println("ErrorCode: " + e.getErrorCode()); System.err.println("SQLState: " + e.getSQLState()); System.err.println("Message: " + e.getMessage()); System.err.println("NextException: " + e.getNextException()); e.printStackTrace(errLog); System.err.println(); } stderr.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/*from w w w . j a va 2s . com*/ Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); ResultSet resultSet = metadata.getColumns(null, null, "users", null); while (resultSet.next()) { String name = resultSet.getString("COLUMN_NAME"); String type = resultSet.getString("TYPE_NAME"); int size = resultSet.getInt("COLUMN_SIZE"); System.out.println("Column name: [" + name + "]; type: [" + type + "]; size: [" + size + "]"); } connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", ""); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String query = "SELECT id, code, name, quantity, price FROM products"; ResultSet uprs = statement.executeQuery(query); while (uprs.next()) { System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name") + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price")); }/*from ww w. j av a2 s.c om*/ uprs.first(); uprs.updateString("name", "Java"); uprs.updateRow(); uprs.next(); uprs.deleteRow(); uprs.moveToInsertRow(); uprs.updateString("code", "1"); uprs.updateString("name", "Data Structures"); uprs.updateInt("quantity", 1); uprs.updateDouble("price", 5.99); uprs.insertRow(); uprs.beforeFirst(); while (uprs.next()) { System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name") + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price")); } connection.close(); }
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); }// w ww.j a va 2 s . c om 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) { Connection conn = null;/*from w ww. j a va2s . co m*/ Statement stmt = null; try { // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; stmt.executeUpdate( "CREATE TABLE Employees ( id INTEGER IDENTITY, first VARCHAR(256), last VARCHAR(256),age INTEGER)"); stmt.executeUpdate("INSERT INTO Employees VALUES(1,'Jack','Smith', 100)"); ResultSet rs = stmt.executeQuery(sql); // Extract data from result set while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } // Clean-up environment rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } System.out.println("Goodbye!"); }