Android - classnotfound使用jdbc连接到MSSQL

时间:2012-11-22 11:20:27

标签: java android sql-server jdbc classnotfoundexception

我正在尝试创建一个从MSSQL Server 2008读取数据的Android应用程序。我发现an example关于使用JDBC,但它不适用于我。< / p>

我跟随罗杰加尔扎的this post回答,但是仍未找到班级

我无法导入图片到帖子,但我在Android Dependencies下有jtds-1.3.0.jar。

我的代码是:

public void query2()
{
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();

String connString = "jdbc:jtds:sqlserver://MYIPSERVER:1433/SERVERNAME;encrypt=false;user=sa;password=password;instance=SQLEXPRESS";
String username = "sa";
String password = "password";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select TOP 10 * from MyTable ORDER BY Transac DESC");

//Print the data to the console
while(reset.next()){
Log.w("Data:",reset.getString(3));
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log.w("Data",reset.getString(2));
}
conn.close();

} catch (Exception e)
{
Log.w("Error connection","");
e.printStackTrace();
}
}

错误就在这一行之后 - Class.forName(driver).newInstance(); - 因为我已经注释了代码并且我有相同的错误日志。 logcat是:

11-21 16:05:55.480: I/Android(1679):  MySQL Connect Example.
11-21 16:05:55.499: W/System.err(1679): java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
11-21 16:05:55.499: W/System.err(1679):     at java.lang.Class.classForName(Native Method)
11-21 16:05:55.509: W/System.err(1679):     at java.lang.Class.forName(Class.java:217)
11-21 16:05:55.509: W/System.err(1679):     at java.lang.Class.forName(Class.java:172)
11-21 16:05:55.509: W/System.err(1679):     at com.example.holz1.MainActivity.query2(MainActivity.java:46)
11-21 16:05:55.509: W/System.err(1679):     at com.example.holz1.MainActivity.onClick(MainActivity.java:35)
11-21 16:05:55.509: W/System.err(1679):     at java.lang.reflect.Method.invokeNative(Native Method)
11-21 16:05:55.509: W/System.err(1679):     at java.lang.reflect.Method.invoke(Method.java:511)
11-21 16:05:55.519: W/System.err(1679):     at android.view.View$1.onClick(View.java:3039)
11-21 16:05:55.519: W/System.err(1679):     at android.view.View.performClick(View.java:3511)
11-21 16:05:55.519: W/System.err(1679):     at android.view.View$PerformClick.run(View.java:14105)
11-21 16:05:55.529: W/System.err(1679):     at android.os.Handler.handleCallback(Handler.java:605)
11-21 16:05:55.529: W/System.err(1679):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-21 16:05:55.543: W/System.err(1679):     at android.os.Looper.loop(Looper.java:137)
11-21 16:05:55.543: W/System.err(1679):     at android.app.ActivityThread.main(ActivityThread.java:4424)
11-21 16:05:55.543: W/System.err(1679):     at java.lang.reflect.Method.invokeNative(Native Method)
11-21 16:05:55.549: W/System.err(1679):     at java.lang.reflect.Method.invoke(Method.java:511)
11-21 16:05:55.549: W/System.err(1679):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-21 16:05:55.549: W/System.err(1679):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-21 16:05:55.549: W/System.err(1679):     at dalvik.system.NativeStart.main(Native Method)
11-21 16:05:55.599: W/System.err(1679): Caused by: java.lang.NoClassDefFoundError: net/sourceforge/jtds/jdbc/Driver
11-21 16:05:55.599: W/System.err(1679):     ... 19 more
11-21 16:05:55.610: W/System.err(1679): Caused by: java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
11-21 16:05:55.619: W/System.err(1679):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-21 16:05:55.619: W/System.err(1679):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-21 16:05:55.629: W/System.err(1679):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-21 16:05:55.629: W/System.err(1679):     ... 19 more
11-21 16:06:51.519: W/WindowManager(90): Failure taking screenshot for (180x300) to layer 21010
11-21 16:06:51.552: W/NetworkManagementSocketTagger(90): setKernelCountSet(10013, 1) failed with errno -2
11-21 16:06:52.499: W/NetworkManagementSocketTagger(90): setKernelCountSet(10047, 0) failed with errno -2

1 个答案:

答案 0 :(得分:0)

我认为问题是由于jar造成的,请下载 sqljdbc-1.2.0.jar 并将其放在根目录的lib文件夹中

download sqljdbc-1.2.0.jar