简体   繁体   中英

Jar file won't recognize mysql database

I made a swing application in Eclipse and it works and compiles fine in Eclipse. It's connecting to mysql. The problem is when I turned it into a jar file the database won't connect. When the application is supposed to load the information from the db, nothing is happening.

public boolean deleteAttendance(String idno,String eventtitle)
{
    int x;
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection(dbUrl, "","");
        String query = "DELETE FROM attendance WHERE id =? AND title = ?";
        PreparedStatement pstmt = con.prepareStatement(query);

        pstmt.setString(1, idno);
        pstmt.setString(2, eventtitle);
        x = pstmt.executeUpdate();

        if(x != 0)
        return true;
        else
            return false;
    }catch(Exception e)
    {
        System.out.println("Statement Error " + e);
    }   
    return false;
}

Just to check whether it is the problem with your required jars not on the class path

Go to window -> preferences->java->build path->user Libraries. add your jar to new user library. Now create a new java project in eclipse right click on this project and select add libraries and add the library your just added in your eclipse . Also add required libraries for database (if any) to your project. If on running your jar everything is working fine then surely its the problem of you not adding ODBC/JDBC driver to your class path else its a problem with your jar .

You would need to make sure that the ODBC driver is in your classpath.

Eclipse does a good job of making sure that your driver is there (because you have to include your dependency jars or else get errors trying to use them).

But once you leave Eclipse - you have to do it yourself.

Is it in your classpath?

To fix this problem, go to artifact and add ojdbc.jar and click OK. If true then nothing, if not then re-create the jar file. note:Just make sure ojdbc.jar is added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM