Steps Making JDBC connection

Steps for Making JDBC connection -
    There are six steps for making the JDBC connection.

1) Load the driver -
    Ex -
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
    conn = DriverManager.getConnection(“<<DSNName>>”,<< username>>, <<password>>);   

It can gives the ClassNotFoundException, SQLException exceptions so use try catch block for this code.

 Try {
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
   
}catch (ClassNotFoundException e) {
                // Could not find the database driver
            } catch (SQLException e) {
                // Could not connect to the database
            }
            catch(Exception e)
            {
                System.err.println("Cannot connect to database server");
                e.printStackTrace();
            }

2) Creating the connection -
    Connection conn = DriverManager.getConnection(“<<DSNName>>”,<< username>>, <<password>>);   

3) Making Statement -
    Statement st =  conn.createStatement();

4) Executing a SQL statement -
    boolean b = st.execute(); or
    int i = st.executeUpdate(); or
    ResultSet rs = st.executeQuery();

5) Get Data From ResultSet -
    while(rs.next()){
        rs.getXXX();
    }
6) Close All Objects -
     rs.close();
    st.close();
    conn.close();

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

Shallow Copy Deep Copy Java example