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 {
          catch(ClassNotFoundException e) {
             System.out.println("Class not found "+ e);
          try {
             Connection connection = DriverManager.getConnection
             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:

Best Client Optimized Programming Language for Mobile Development?

Use Google's Dart language, which is Object-oriented, class-based and garbage-collected language with C-Style.  Dart can compile to eith...