{{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:

Generate Models from SQL Server using Entity Framework Core

To generate models from SQL Server database tables using Entity Framework (EF) in .NET, you can follow the Database-First approach with Ent...