• Java DataBase Connectivity (JDBC)-Example

    Java DataBase Connectivity (JDBC)-Example

    Here I have created simple Java DataBase Connectivity (JDBC) example that show you the use of JDBC connection with several DataBase operations like, View, Insert, Save, Update, and Delete.

    JDBC Example Figure

    JDBC Example Figure

    Example-JDBC Program Code

    Example:

    1. To Start a practical, we need to first create ODBC connection. If you are using Windows OS then go to Control Panel à Administrative Tools à ODBC Data Source and create desired DSN (Data Source Name). Here I have created “EmpDSN”.
    2. Now Start NetBeans IDE. Create new java application and draw the frame as displayed below figure.
    3. After completing the design, click on source tab and start to declare variable in general declaration under class area.
    String dbUrl = “jdbc:odbc:EmpDSN”;String user = “”;String pass = “”;

    Connection c;

    Statement s;

    ResultSet r;

    1. Now write down necessary code into class constructor which are need to initialize once like, calling Class.forName() method, initialize connection, creating ResultSet.
    public Practical01DBConnection() {initComponents();try{

    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

    c = DriverManager.getConnection(dbUrl, user, pass);

    s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

    r = s.executeQuery(“Select * From Employee”);

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    1. Write down the operation code on ActionPerformed event of appropriate button Like, Insert Button, Delete Button, Save Button, Update Button, and View Button.
    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:try{

    r = s.executeQuery(“Select * From Employee”);

    r.first();

     

    tfENo.setText(r.getString(“ENo”));

    tfEName.setText(r.getString(“EName”));

    tfEDesignation.setText(r.getString(“EDesignation”));

    tfECity.setText(r.getString(“ECity”));

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    tfENo.setText(“”);

    tfEName.setText(“”);

    tfEDesignation.setText(“”);

    tfECity.setText(“”);

    }

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    String ENo = tfENo.getText();

    String EName = tfEName.getText();

    String EDesig = tfEDesignation.getText();

    String ECity = tfECity.getText();

    String insEmp = “Insert Into Employee Values(‘” + ENo + “‘,'” +

    EName + “‘,'” + EDesig + “‘,'” + ECity + “‘)”;

    try{

    s.executeUpdate(insEmp);

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    String ENo = tfENo.getText();

    String EName = tfEName.getText();

    String EDesig = tfEDesignation.getText();

    String ECity = tfECity.getText();

    String updtEmp = “Update Employee set EName = ‘” + EName + “‘, EDesignation = ‘” +

    EDesig + “‘, ECity = ‘” + ECity + “‘ where ENo = ‘” + ENo + “‘”;

    try{

    s.executeUpdate(updtEmp);

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    String ENo = tfENo.getText();

    String delEmp = “Delete From Employee Where ENo = ‘” + ENo + “‘”;

    try{

    s.executeUpdate(delEmp);

    r.first();

    tfENo.setText(r.getString(“ENo”));

    tfEName.setText(r.getString(“EName”));

    tfEDesignation.setText(r.getString(“EDesignation”));

    tfECity.setText(r.getString(“ECity”));

    }catch(Exception e){}

    }

    1. Write down the navigation code on ActionPerformed event of appropriate button Like, Move First, Move Previous, Move Next, and Move Last.
    private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:try{

    r.first();

    tfENo.setText(r.getString(“ENo”));

    tfEName.setText(r.getString(“EName”));

    tfEDesignation.setText(r.getString(“EDesignation”));

    tfECity.setText(r.getString(“ECity”));

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    try{

    r.previous();

    if(r.isBeforeFirst()){

    r.first();

    }

    tfENo.setText(r.getString(“ENo”));

    tfEName.setText(r.getString(“EName”));

    tfEDesignation.setText(r.getString(“EDesignation”));

    tfECity.setText(r.getString(“ECity”));

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    try{

    r.next();

    if(r.isAfterLast()){

    r.last();

    }

    tfENo.setText(r.getString(“ENo”));

    tfEName.setText(r.getString(“EName”));

    tfEDesignation.setText(r.getString(“EDesignation”));

    tfECity.setText(r.getString(“ECity”));

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {

    // TODO add your handling code here:

    try{

    r.last();

    tfENo.setText(r.getString(“ENo”));

    tfEName.setText(r.getString(“EName”));

    tfEDesignation.setText(r.getString(“EDesignation”));

    tfECity.setText(r.getString(“ECity”));

    }catch(Exception e){

    System.out.println(e.toString());

    }

    }

    1. Now Save and Run Application.
    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *