0

I have been trying to connect Database in Java for a quit time now. Following is the code. I have worked with .mdb extension, but this time I am using .accdb extension.
Database File.

import java.sql.*;
class DB{
 static Connection con;
 static Statement sta;
 static void getDBConnection(String path)
{
    try{
        System.out.println("-----Requesting the Connection---------");
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con=DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+path);
        sta=con.createStatement();
        System.out.println("------------Connection Established");
    }
    catch(SQLException e)
    {
        System.out.println(e);
        e.printStackTrace();
    }
    catch(ClassNotFoundException e){
        System.out.println(e);
        e.printStackTrace();
    }
    }
public static Statement getStatement(){
    return sta;
}
public static void disconnectDB(){
    try{
        con.close();
        System.out.print("------Connection closed----");
    }
    catch(SQLException e){
        System.out.println(e);
        e.printStackTrace();
    }
}
}

Item File.

import java.sql.Statement;
import java.sql.ResultSet;
 class item
 {
    public void viewItem()
    {
        int col1Value=0;
        String col2Value=null;
        int col3Value=0;
        double col4Value=0.0;
        double total=col3Value*col4Value;
        DB dbConnect = new DB();
        dbConnect.getDBConnection("ZTBL.accdb");
        Statement statement = dbConnect.getStatement();
         String query = "SELECT * FROM item";
         try
         {
             ResultSet resultSet = statement.executeQuery(query);
             while (resultSet.next())
             {
                 col1Value = resultSet.getInt("Item Id");
                 col2Value = resultSet.getString("Item Name");
                 col3Value = resultSet.getInt("Quantity");
                 col4Value = resultSet.getDouble("Total Amount");
        /* Printing the data on the console */
        System.out.println(col1Value+ "    "+col2Value+"   "+col3Value+"  "+col4Value+"  "+total);
      } //end of while
    }//end of try
    catch(Exception exception)
    {
 	System.out.println("Exception while executing the Query: "+exception);
    }
    finally
    {
        dbConnect.disconnectDB();
    }
    }
 }

and Client file.

import java.sql.Statement;
import java.sql.ResultSet;
public class client
{
    public static void main(String[] args)
    {
            item a = new item();
            a.viewItem();
           // stock b=new stock();
            //b.viewstock();
    }
}

Please guys help me out!

Edited by ahmedshayan: n/a

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by NormR1
0

Please explain what the problem is.
If you are getting errors, copy and paste the full text of the messages here.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.