List of usage examples for java.sql ResultSet next
boolean next() throws SQLException;
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 .ja v a 2 s.co m*/ 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); DatabaseMetaData dbmd = connection.getMetaData(); ResultSet resultSet = dbmd.getTypeInfo(); while (resultSet.next()) { // Get the database-specific type name String typeName = resultSet.getString("TYPE_NAME"); // Get the java.sql.Types type to which this database-specific type is mapped short dataType = resultSet.getShort("DATA_TYPE"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { int sum = 0;//ww w . j a va 2 s .co m Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT SUM(col) FROM mytable"); while (res.next()) { int c = res.getInt(1); sum = sum + c; } System.out.println("Sum of column = " + sum); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); DatabaseMetaData md = conn.getMetaData(); ResultSet r = md.getColumns("", "", "yourTableName", "%"); while (r.next()) System.out.println("\t" + r.getString(4) + " : " + r.getString(6)); conn.close();// www . j a v a 2s.c om }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Class.forName(driver).newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM emp"); while (res.next()) { int i = res.getInt("ID"); String s = res.getString("name"); System.out.println(i + "\t\t" + s); }//from w w w . java 2s . c o m con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection(url, username, password); PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents "); ResultSet resultSet = stmt.executeQuery(); while (resultSet.next()) { String name = resultSet.getString(1); String description = resultSet.getString(2); File data = new File("C:\\a.txt"); Reader reader = resultSet.getCharacterStream(3); FileWriter writer = new FileWriter(data); char[] buffer = new char[1]; while (reader.read(buffer) > 0) { writer.write(buffer);/*from w w w . ja va 2s . com*/ } writer.close(); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection(url, username, password); DatabaseMetaData metadata = conn.getMetaData(); ResultSet result = metadata.getProcedures(null, "JAVA", "%"); while (result.next()) { System.out.println(result.getString("PROCEDURE_CAT") + " - " + result.getString("PROCEDURE_SCHEM") + " - " + result.getString("PROCEDURE_NAME")); }//from ww w .ja v a 2 s .c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); DatabaseMetaData dbmd = conn.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { String typeName = rs.getString("TYPE_NAME"); short dataType = rs.getShort("DATA_TYPE"); String createParams = rs.getString("CREATE_PARAMS"); int nullable = rs.getInt("NULLABLE"); boolean caseSensitive = rs.getBoolean("CASE_SENSITIVE"); System.out.println("DBMS type " + typeName + ":"); System.out.println(" java.sql.Types: " + dataType); System.out.print(" parameters used to create: "); System.out.println(createParams); System.out.println(" nullable?: " + nullable); System.out.print(" case sensitive?: "); System.out.println(caseSensitive); System.out.println(""); }//from w w w .j a va 2 s . c o m conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection m_Connection = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase", "userid", "password"); Statement m_Statement = m_Connection.createStatement(); String query = "SELECT * FROM MyTable"; ResultSet m_ResultSet = m_Statement.executeQuery(query); while (m_ResultSet.next()) { System.out.println(//w w w .ja va2s.co m m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", " + m_ResultSet.getString(3)); } }
From source file:Main.java
public static void main(String[] args) throws Exception { System.out.println("Count number of rows in a specific table!"); Connection con = null;/* w w w. j a v a 2 s .c o m*/ int count = 0; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); ResultSet res = st.executeQuery("SELECT COUNT(*) FROM EMP"); while (res.next()) { count = res.getInt(1); } System.out.println("Number of row:" + count); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection(url, username, password); String sql = "SELECT name, description, image FROM pictures "; PreparedStatement stmt = conn.prepareStatement(sql); ResultSet resultSet = stmt.executeQuery(); while (resultSet.next()) { String name = resultSet.getString(1); String description = resultSet.getString(2); File image = new File("D:\\java.gif"); FileOutputStream fos = new FileOutputStream(image); byte[] buffer = new byte[1]; InputStream is = resultSet.getBinaryStream(3); while (is.read(buffer) > 0) { fos.write(buffer);/*from ww w . j a va 2 s . c om*/ } fos.close(); } conn.close(); }