0

我正在尝试制作一个从MSSQLServer 2008 读取数据的 android 应用程序。我找到了一个关于 using的示例JDBC,但它对我不起作用。

我已经从 Roger Garza 的这篇文章中得到了答案,但仍然找不到该课程

我无法导入要发布的图像,但我在 Android 依赖项下有 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));
//              Log.w("Data",reset.getString(2));
}
conn.close();

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

错误就在这一行之后Class.forName(driver).newInstance();——因为我在之后评论了代码并且我有相同的错误日志。日志是:

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
4

1 回答 1

0

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

下载sqljdbc-1.2.0.jar

于 2012-11-22T11:24:52.280 回答