2

i'm new to java, and i want to learn about logging in to java application, i have made the following ConnectAccess class to connect with Microsoft Access

import java.sql.*;
public class ConnectAccess {
public static Connection con = null;
public static Statement stmt = null;
//public static String DBase =  "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=1401087265db.mdb;";
public static String DBase =  "jdbc:odbc:datasource_access";
public static ResultSet rs = null;

public ConnectAccess() {
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        try {
            Connection conn = DriverManager.getConnection(DBase, "", "");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

public ResultSet getData(String Query) //provide quickfix eclipse
{       
    try {               
        stmt.execute(Query);            
        rs=stmt.getResultSet();         
        if(rs!=null && rs.next())
        {
            return rs;
        }
        else
        {
            return null;
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;    
}

public void deleteData(String primarykey,String primarykeyvalue, String table)
{
    try {
        stmt.executeUpdate("Delete from "+table+"where "+primarykey+" = '"+primarykeyvalue+"'");
        stmt.close();
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

}

and here's code in my Login button,

            ConnectAccess ca = new ConnectAccess();
            ResultSet resultnya = ca.getData("Select * from MsUser where Username = '"+inusernamenya+"' and password = '"+inpassword+"'");
            try {
                if(resultnya.next())
                {
                    cmd.printSuccess(inusernamenya+" dan "+inpassword+" berhasil login", form_name);                            
                    Cl_Main mainform = new Cl_Main();                           
                }
                else
                {
                    cmd.printError("Invalid username or password !", form_name);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

i don't know if java unable to find my DB location, or is there something wrong with my java build path, Ms Access Location java build path

how do i resolve this problem ?

update : i've update with my stacktrace here

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ConnectAccess.<init>(ConnectAccess.java:13)
    at Cl_Login$1.actionPerformed(Cl_Login.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

and eclipse show me a new tab . . . error

update : I have set ODBC user dsn and system dsn from SysWow\odbc32`` enter image description here

and another stacktrace error

java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ConnectAccess.<init>(ConnectAccess.java:14)
    at Cl_Login$1.actionPerformed(Cl_Login.java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Cignitor
  • 891
  • 3
  • 16
  • 36
  • You haven't really told us what the problem is. Are you getting an exception? If so show us the stack trace. – greg-449 Nov 18 '15 at 08:07
  • @greg-449 i forgot it, i've updated the thread with my stacktrace – Cignitor Nov 18 '15 at 13:47
  • 1
    Consider the [UCanAccess library](http://ucanaccess.sourceforge.net/site.html) for Java connections to MS Access. As of JDK 1.8, the [jdbc-odbc bridge](http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html) will no longer be supported. – Parfait Nov 18 '15 at 23:58

1 Answers1

0

I think the DBase value is wrong. Can you try DBase = "jdbc:odbc:xxxxx"; where xxxxxx is the name of an ODBC data source, which you set up using standard windows ODBC data source administrator. There you can say you want an MS Access driver etc.

For an example, look here: http://www.interfaceware.com/manual/setting_up_odbc_datasource.html

Please note though that the JDBC-ODBC bridge you're using is not supported by Oracle, and will be abandoned. See https://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/bridge.html

geert3
  • 7,086
  • 1
  • 33
  • 49
  • i have updated my current status, and i have set my ODBC driver – Cignitor Nov 18 '15 at 14:49
  • Looks better after your update. Likely now you used the wrong ODBC data source manager. Microsoft has a 64 bit and 32 bit version. To manage a data source that connects to a 32-bit driver under 64-bit platform, use `c:\windows\sysWOW64\odbcad32.exe`. To manage a data source that connects to a 64-bit driver, use `c:\windows\system32\odbcad32.exe`, so just remove the DSN in the one and add it in the other. – geert3 Nov 18 '15 at 15:13
  • yep, the one that i settled above is located on `SysWow64` i can't find any ODBC driver when i use `System32\odbcad32.exe` . how do i know if my application is using 32bit-driver or 64bit-driver ? – Cignitor Nov 18 '15 at 16:06
  • I don't know, I have MS Access ODBC drivers in both versions, likely they are there just from installing MS Access. – geert3 Nov 18 '15 at 16:19
  • To OP - it depends on your Java [installation](http://stackoverflow.com/questions/2062020/how-can-i-tell-if-im-running-in-64-bit-jvm-or-32-bit-jvm-from-within-a-program), 32-bit or 64-bit. What version of Windows are you running? Screenshots seem earlier than XP which may explain missing System32. There is no 64-bit version for Windows 2000. – Parfait Nov 19 '15 at 01:16
  • @Parfait i am using windows 7 64 bit. currently i'm going to download `Ms Access` 32bit driver – Cignitor Nov 19 '15 at 04:27