List of usage examples for java.sql ResultSet getString
String getString(String columnLabel) throws SQLException;
ResultSet
object as a String
in the Java programming language. From source file:Main.java
public static void main(String[] args) throws Exception { String hostname = "", dbname = "", username = "", password = ""; Class.forName("org.postgresql.Driver"); Connection connection = DriverManager.getConnection("jdbc:postgresql://" + hostname + "/" + dbname, username, password);// www.ja va 2 s . c o m ResultSet rs = connection.createStatement().executeQuery("select version() as version"); while (rs.next()) { System.out.println(rs.getString("version")); } }
From source file:com.alibaba.cobar.manager.test.ConnectionTest.java
/** * @param args/*from ww w.j a va2 s . c o m*/ */ public static void main(String[] args) { try { BasicDataSource ds = new BasicDataSource(); ds.setUsername("test"); ds.setPassword(""); ds.setUrl("jdbc:mysql://10.20.153.178:9066/"); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setMaxActive(-1); ds.setMinIdle(0); ds.setTimeBetweenEvictionRunsMillis(600000); ds.setNumTestsPerEvictionRun(Integer.MAX_VALUE); ds.setMinEvictableIdleTimeMillis(GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); Connection conn = ds.getConnection(); Statement stm = conn.createStatement(); stm.execute("show @@version"); ResultSet rst = stm.getResultSet(); rst.next(); String version = rst.getString("VERSION"); System.out.println(version); } catch (Exception exception) { System.out.println("10.20.153.178:9066 " + exception.getMessage() + exception); } }
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();/*w w w . j a v a 2 s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString(2); if (rs.wasNull()) { System.out.println("was NULL"); } else { System.out.println("not NULL"); } } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, id2 tinyint, id3 smallint, id4 bigint, id5 real);"); String sql = "INSERT INTO survey (id2,id3,id4,id5) VALUES(?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); byte b = 1;//from ww w . j ava 2 s. c o m short s = 2; pstmt.setByte(1, b); pstmt.setShort(2, s); pstmt.setInt(3, 3); pstmt.setLong(4, 4L); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER);/* w w w . java2 s. c o m*/ 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 { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id DECIMAL, name BINARY );"); String sql = "INSERT INTO survey (id) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setBigDecimal(1, new BigDecimal("1.00000")); // insert the data pstmt.executeUpdate();// ww w. ja v a 2 s.c o m ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(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 ww w.ja v a 2 s . c om*/ 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:Test.java
public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/contact", "userName", "password"); DatabaseMetaData metaData = con.getMetaData(); ResultSet resultSet = metaData.getPseudoColumns("", "schemaName", "tableName", ""); while (resultSet.next()) { System.out.println(resultSet.getString("TABLE_SCHEM ") + " - " + resultSet.getString("COLUMN_NAME ")); }/*w ww . j a va 2s. c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, register int );"); String sql = "INSERT INTO survey (register) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setBoolean(1, true);//from w w w .j av a 2 s .c o m pstmt.executeUpdate(); pstmt.setBoolean(1, false); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } rs.close(); stmt.close(); conn.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_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); while (resultSet.next()) { String productCode = resultSet.getString("product_code"); int row = resultSet.getRow(); System.out.println(row + ". " + productCode); }//from www. j av a 2 s .c o m connection.close(); }