我直接将 android 连接到 SQLServer(我知道它不推荐)
我写了以下代码:
public class MainActivity extends Activity {
String dbName = "AndroidDB";
String serverip="10.0.2.2";
String serverport="1433";
//String url = "jdbc:sqlserver://14GRAFICALI\\MSSQLSERVER2008;databaseName="+dbName+"";
//String url ="jdbc:sqlserver://14GRAFICALI\\MSSQLSERVER2008;databaseName=AndroidDB;integratedSecurity=true";
String url ="jdbc:sqlserver://14GRAFICALI;instanceName=\\MSSQLSERVER2008;DatabaseName=AndroidDB;integratedSecurity=true";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvData=(TextView)findViewById(R.id.tvSelectedData);
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Class.forName("javax.sql.XAConnection").newInstance();
Connection conn =DriverManager.getConnection(url);
Statement statement=conn.createStatement();
ResultSet resultSet=statement.executeQuery("select * from UserMaster");
while(resultSet.next()){
tvData.setText(" Data1 : "+resultSet.getString(1)+" Data 2 : "+resultSet.getNString(2));
}
} catch (Exception e) {
e.printStackTrace();
tvData.setText(e.getMessage());
}
}
在此之前我没有这条线:
Class.forName("javax.sql.XAConnection").newInstance();
但它给了我一个例外:
09-07 15:12:20.911: E/dalvikvm(1293): Could not find class 'javax.sql.XAConnection', referenced from method com.microsoft.sqlserver.jdbc.SQLServerConnection.poolCloseEventNotify
因此,我导入了一个包含此文件的 jar 并将其添加到库中:
(jboss.jar)
出口:
在此之后我也收到错误:
java.lang.ClassNotFoundException: javax.sql.XAConnection
我有这门课:
我不明白为什么会出现这个错误。请帮我。
日志猫:
09-07 15:52:52.641: W/System.err(330): java.lang.ClassNotFoundException: javax.sql.XAConnection
09-07 15:52:52.641: W/System.err(330): at java.lang.Class.classForName(Native Method)
09-07 15:52:52.641: W/System.err(330): at java.lang.Class.forName(Class.java:234)
09-07 15:52:52.641: W/System.err(330): at java.lang.Class.forName(Class.java:181)
09-07 15:52:52.641: W/System.err(330): at com.example.sqlservercall.MainActivity.onCreate(MainActivity.java:33)
09-07 15:52:52.641: W/System.err(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 15:52:52.651: W/System.err(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-07 15:52:52.651: W/System.err(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-07 15:52:52.651: W/System.err(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-07 15:52:52.651: W/System.err(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-07 15:52:52.651: W/System.err(330): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 15:52:52.651: W/System.err(330): at android.os.Looper.loop(Looper.java:123)
09-07 15:52:52.651: W/System.err(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-07 15:52:52.651: W/System.err(330): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 15:52:52.651: W/System.err(330): at java.lang.reflect.Method.invoke(Method.java:507)
09-07 15:52:52.651: W/System.err(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-07 15:52:52.651: W/System.err(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-07 15:52:52.651: W/System.err(330): at dalvik.system.NativeStart.main(Native Method)
09-07 15:52:52.651: W/System.err(330): Caused by: java.lang.NoClassDefFoundError: javax.sql.XAConnection
09-07 15:52:52.661: W/System.err(330): ... 17 more
09-07 15:52:52.661: W/System.err(330): Caused by: java.lang.ClassNotFoundException: javax.sql.XAConnection in loader dalvik.system.PathClassLoader[/data/app/com.example.sqlservercall-2.apk]
09-07 15:52:52.661: W/System.err(330): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-07 15:52:52.661: W/System.err(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-07 15:52:52.671: W/System.err(330): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-07 15:52:52.671: W/System.err(330): ... 17 more
09-07 15:52:52.861: I/ActivityManager(61): Displayed com.example.sqlservercall/.MainActivity: +1s503ms
09-07 15:53:00.081: W/ActivityManager(61): finishReceiver called but no pending broadcasts