{{theTime}}

Search This Blog

Total Pageviews

Program to Retrieve Table Contents using JDBC


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * -- Use try-resource to auto close the resources.  
 *
 */
public class RetrieveTableContents
{
       public static void main(String[] args) {
          try {
             Class.forName("org.apache.derby.jdbc.ClientDriver");
          }
          catch(ClassNotFoundException e) {
             System.out.println("Class not found "+ e);
          }
          try {
             Connection connection = DriverManager.getConnection
             ("jdbc:derby://localhost:143/testDb","username",
             "password");
             try( Statement stmt = connection.createStatement()){
                 ResultSet rs = stmt.executeQuery
                         ("SELECT * FROM Table_name");
                 System.out.println("id  name    job");
                 while (rs.next()) {
                     int id = rs.getInt("id");
                     String col1 = rs.getString("ColName");
                     String col2 = rs.getString("ColName");
                     System.out.println(id+"   "+col1+"    "+col2);
                 }
             }
          }
          catch(SQLException e){
             System.out.println("SQL exception occured" + e);
          }
       }
}

No comments:

Windows 8.3 file naming setting is disabled for the product

To enable Windows 8.3 file naming setting use fsutil command Example:  c:\> fsutil 8dot3name set C: 0