class not found exception in android

2020-02-16 02:32发布

问题:

I am using following code for SQLConnection in android:

public void queryResultSet(String CommandSQL) throws ClassNotFoundException, java.sql.SQLException
    {

        ResultSet rs;
        Connection conn = null;

            Toast msg1 = Toast.makeText(getBaseContext(),
                    "Name = " + CommandSQL, Toast.LENGTH_LONG);
            msg1.show();

             Class.forName("net.sourceforge.jtds.jdbc.Driver");
             String username="14Graficali\\Administrator";
             String ConnUrl="jdbc:jtds:sqlserver://14GRAFICALI\\MSSQLSERVER2008;DatabaseName=DVDKiosk;user=14Graficali\\Administrator;Instance=SQLExpress";
             conn = DriverManager.getConnection(ConnUrl);

            Statement st=conn.createStatement();
            rs=st.executeQuery(CommandSQL);
            while(rs.next())
            {

                Toast msg2 = Toast.makeText(getBaseContext(),
                        "Name = " + rs.getString("UserName"), Toast.LENGTH_LONG);
                msg2.show();
            }

    }

I have included net.sourceforge.jtds.jdbc.Driver as we can see below :

Stack Trace:

08-20 10:57:42.087: W/System.err(400): java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
08-20 10:57:42.087: W/System.err(400):  at java.lang.Class.classForName(Native Method)
08-20 10:57:42.087: W/System.err(400):  at java.lang.Class.forName(Class.java:234)
08-20 10:57:42.087: W/System.err(400):  at java.lang.Class.forName(Class.java:181)
08-20 10:57:42.087: W/System.err(400):  at com.example.registrationapp.Login.queryResultSet(Login.java:93)
08-20 10:57:42.087: W/System.err(400):  at com.example.registrationapp.Login$1.onClick(Login.java:126)
08-20 10:57:42.087: W/System.err(400):  at android.view.View.performClick(View.java:2485)
08-20 10:57:42.087: W/System.err(400):  at android.view.View$PerformClick.run(View.java:9080)
08-20 10:57:42.087: W/System.err(400):  at android.os.Handler.handleCallback(Handler.java:587)
08-20 10:57:42.087: W/System.err(400):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 10:57:42.098: W/System.err(400):  at android.os.Looper.loop(Looper.java:123)
08-20 10:57:42.098: W/System.err(400):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-20 10:57:42.098: W/System.err(400):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 10:57:42.098: W/System.err(400):  at java.lang.reflect.Method.invoke(Method.java:507)
08-20 10:57:42.098: W/System.err(400):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-20 10:57:42.098: W/System.err(400):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-20 10:57:42.098: W/System.err(400):  at dalvik.system.NativeStart.main(Native Method)
08-20 10:57:42.098: W/System.err(400): Caused by: java.lang.NoClassDefFoundError: net.sourceforge.jtds.jdbc.Driver
08-20 10:57:42.107: W/System.err(400):  ... 16 more
08-20 10:57:42.107: W/System.err(400): Caused by: java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver in loader dalvik.system.PathClassLoader[/data/app/com.example.registrationapp-1.apk]
08-20 10:57:42.107: W/System.err(400):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
08-20 10:57:42.107: W/System.err(400):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
08-20 10:57:42.107: W/System.err(400):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)

Please help me.

回答1:

If you have already added the jtds-1.2.5 driver, good. Otherwise:

Right click on the Project > Properties > Libraries Tab Add the jtds-1.2.5.jar file by clicking on "Add External JARs"

Next, in the Order and Export tab (Right next to Libraries in the Java Build Path window) check the box for jtds-1.2.5.jar (and any other external jars you might have).

This will export the .jar containing the driver with the .apk file.



回答2:

If you use ProGuard, then your class may be dropped by it because it is used with reflection only. And if you don't, Android can still fail to load class for some reasons. Look at LogCat during your package installation.



回答3:

right click on the project - properties - java build path - Order and export - tick Android private libraries and all the libraries that u added