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:MainClass.java
public static void main(String[] args) { try {//ww w . j a v a 2 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:CallableStmt.java
public static void main(String args[]) throws Exception { String storedProc = "create procedure SHOW_ORDERS_BY_STATE @State CHAR (2) as " + "select c.Last_Name+', '+c.First_Name AS Name,o.Order_Number " + "from CUSTOMERS c, ORDERS o where c.Customer_Number = o.Customer_Number " + "AND c.State = @State order by c.Last_Name;"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Customers"); Statement stmt = con.createStatement(); stmt.executeUpdate(storedProc);/*from ww w.j a v a 2 s. c o m*/ CallableStatement cs = con.prepareCall("{call SHOW_ORDERS_BY_STATE(?)}"); cs.setString(1, "NJ"); ResultSet rs = cs.executeQuery(); while (rs.next()) { String name = rs.getString("Name"); int orderNo = rs.getInt("Order_Number"); System.out.println(name + ": " + orderNo); } }
From source file:Main.java
public static void main(String[] args) throws Exception { // DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "manager"); String plsql = " declare " + " p_id varchar2(20) := null; " + " l_rc sys_refcursor;" + " begin " + " p_id := ?; " + " ? := 'input parameter was = ' || p_id;" + " open l_rc for " + " select 1 id, 'abc' name from dual; " + " ? := l_rc;" + " end;"; CallableStatement cs = c.prepareCall(plsql); cs.setString(1, "12345"); cs.registerOutParameter(2, Types.VARCHAR); // cs.registerOutParameter(3, OracleTypes.CURSOR); cs.execute();/* w w w .j ava 2 s. c o m*/ System.out.println("Result = " + cs.getObject(2)); ResultSet cursorResultSet = (ResultSet) cs.getObject(3); while (cursorResultSet.next()) { System.out.println(cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2)); } cs.close(); c.close(); }
From source file:Main.java
public static void main(String[] args) { Connection conn = null;//from ww w. j a v a 2s .c o m Statement stmt = null; try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { 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); } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { int count = 0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT title,year_made FROM product WHERE year_made >= ? AND year_made <= ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2000);//from w w w.j a v a 2 s.c om prest.setInt(2, 2009); ResultSet rs = prest.executeQuery(); while (rs.next()) { String mov_name = rs.getString(1); int mov_year = rs.getInt(2); count++; System.out.println(mov_name + "\t" + "- " + mov_year); } System.out.println("Number of records: " + count); prest.close(); con.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"); String sql = "SELECT * FROM product WHERE year_made = ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2002);//from w ww .j ava 2 s .c om ResultSet rs1 = prest.executeQuery(); while (rs1.next()) { String mov_name = rs1.getString(1); int mad_year = rs1.getInt(2); System.out.println(mov_name + "\t- " + mad_year); } prest.setInt(1, 2003); ResultSet rs2 = prest.executeQuery(); while (rs2.next()) { String mov_name = rs2.getString(1); int mad_year = rs2.getInt(2); System.out.println(mov_name + "\t- " + mad_year); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from w w w .ja v a2 s . c om*/ Statement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Deleting database..."); stmt = conn.createStatement(); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "SELECT id, first, last, age FROM Person"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("firstName"); String last = rs.getString("lastName"); // Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection dbConnection = null; String myConnectionString = ""; myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb"; dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever"); PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest"); ResultSet rs = stmt.executeQuery(); int i = 0;/*from w w w .ja v a2s . c om*/ int j = 0; String s = ""; while (rs.next()) { i++; j = rs.getInt("id"); s = rs.getString("textcol"); } System.out.println(String.format("Finished reading %d rows.", i)); rs.close(); stmt.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection conn = null;//www . j av a 2 s.c o m try { conn = DriverManager.getConnection(url, username, password); conn.setAutoCommit(false); Statement st = conn.createStatement(); st.execute("INSERT INTO orders (username, order_date) VALUES ('java', '2007-12-13')", Statement.RETURN_GENERATED_KEYS); ResultSet keys = st.getGeneratedKeys(); int id = 1; while (keys.next()) { id = keys.getInt(1); } PreparedStatement pst = conn.prepareStatement( "INSERT INTO order_details (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)"); pst.setInt(1, id); pst.setString(2, "1"); pst.setInt(3, 10); pst.setDouble(4, 100); pst.execute(); conn.commit(); System.out.println("Transaction commit..."); } catch (SQLException e) { if (conn != null) { conn.rollback(); System.out.println("Connection rollback..."); } e.printStackTrace(); } finally { if (conn != null && !conn.isClosed()) { conn.close(); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;/*from w w w .java 2 s. c o m*/ Statement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Deleting database..."); stmt = conn.createStatement(); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "UPDATE Registration SET age = 30 WHERE id in (1, 2)"; stmt.executeUpdate(sql); sql = "SELECT id, first, last, age FROM Person"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { // Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("firstName"); String last = rs.getString("lastName"); // Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } rs.close(); stmt.close(); conn.close(); }