Wednesday, 15 July 2015

Create JDBC Connection Using Derby Database and fetch the table data

  
         
import java.sql.*;

public class JavaDB {

    public static void main(String[] args) {

        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Class not found " + e);
        }
        String DbURL = "jdbc:derby://localhost:1527/test";
        String Uname = "root";
        String Pwd = "root";
        try {
            Connection con = DriverManager.getConnection(DbURL, Uname, Pwd);
            System.out.println("DB Connected");

            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("SELECT NAME FROM MOVIE");

            while (rs.next()) {
                System.out.println("Name: = " + rs.getString("name"));
            }
            con.close();
            System.out.println("DB disconnected");
        } catch (SQLException e) {
            System.out.println("SQL exception occured" + e);
        }
    }
}
        
  

No comments:

Post a Comment