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: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 = conn.createStatement();/*from ww w. j ava2s. c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet while (rs.next()) { int id = rs.getInt("id"); System.out.println("id=" + id); String name = rs.getString(2); System.out.println("name=" + name); if (rs.wasNull()) { System.out.println("name is null"); } else { System.out.println("name is not null"); } System.out.println("---------------"); } 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 = conn.createStatement();/*from w ww . j a va 2 s . com*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet while (rs.next()) { int id = rs.getInt(1); System.out.println("id=" + id); String name = rs.getString(2); System.out.println("name=" + name); if (rs.wasNull()) { System.out.println("name is null"); } else { System.out.println("name is not null"); } System.out.println("---------------"); } 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(); 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 = conn.createStatement();/* www . j a va 2s. com*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet while (rs.next()) { int id = rs.getInt(1); System.out.println("id=" + id); String name = rs.getString(2); System.out.println("name=" + name); if (rs.wasNull()) { System.out.println("name is null"); } else { System.out.println("name is not null"); } System.out.println("---------------"); } rs.close(); st.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);/*w w w .jav a 2 s. c om*/ 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:PreparedStmt.java
public static void main(String args[]) throws Exception { String query = "SELECT * FROM Stock WHERE Item_Number = ?"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory"); PreparedStatement pstmt = con.prepareStatement(query); pstmt.setInt(1, 2);//from w ww . jav a 2 s. c o m ResultSet rs = pstmt.executeQuery(); while (rs.next()) { String name = rs.getString("Name"); String desc = rs.getString("Description"); int qty = rs.getInt("Qty"); float cost = rs.getFloat("Cost"); System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost); } }
From source file:JdbcDemo.java
public static void main(String args[]) throws Exception { String query = "SELECT Name,Description,Qty,Cost FROM Stock"; 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"); System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost); }/* ww w .ja v a 2s. com*/ con.close(); }
From source file:Employee.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); Map map = conn.getTypeMap();//from w w w . j av a 2 s .com map.put("EMP_DATA", Class.forName("Employee")); conn.setTypeMap(map); ResultSet rs = stmt.executeQuery("SELECT * from Emp"); Employee employee; while (rs.next()) { int empId = rs.getInt("EmpId"); employee = (Employee) rs.getObject("Emp_Info"); System.out.print("Employee Id: " + empId + ", SSN: " + employee.SSN); System.out.print(", Name: " + employee.FirstName + " " + employee.LastName); System.out.println( ", Yearly Salary: $" + employee.Salary + " Monthly Salary: " + employee.calcMonthlySalary()); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Statement stmt = DriverManager.getConnection("jdbc:odbc:employee").createStatement(); ResultSet rs = stmt.executeQuery("select lastname, firstname, id from [Sheet1$]"); while (rs.next()) { String lname = rs.getString(1); String fname = rs.getString(2); int id = rs.getInt(3); System.out.println(fname + " " + lname + " id : " + id); }//ww w. j a va 2 s . c o m rs.close(); stmt.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:odbc:technical_library"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String theStatement = "SELECT authid, lastname, firstname, email FROM authors ORDER BY authid"; try {//from w w w.j a va 2 s. com Class.forName(driver); Connection connection = DriverManager.getConnection(url, "guest", "guest"); Statement queryAuthors = connection.createStatement(); ResultSet results = queryAuthors.executeQuery(theStatement); String lastname, firstname, email; int id; while (results.next()) { id = results.getInt(1); lastname = results.getString(2); firstname = results.getString(3); email = results.getString(4); if (results.wasNull()) { email = "no email"; } System.out.println(Integer.toString(id) + ", " + lastname.trim() + ", " + firstname.trim() + ", " + email.trim()); } queryAuthors.close(); } catch (Exception e) { System.err.println(e); } }
From source file:PrintResultSet.java
public static void main(String args[]) throws Exception { String query = "SELECT Name,Description,Qty,Cost FROM Stock"; 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()) { System.out.print(rs.getString("Name") + "\t"); System.out.print(rs.getString("Description") + "\t"); System.out.print(rs.getInt("Qty") + "\t"); System.out.println(rs.getFloat("Cost")); }/*from w ww . j a v a 2 s .c o m*/ }