我直接将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
我有这门课:
我不明白为什么会出现这个错误.请帮我.
logcat的:
09-07 15:52:52.641: W/System.err(330): java.lang.classNotFoundException: javax.sql.XAConnection09-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.access00(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.XAConnection09-07 15:52:52.661: W/System.err(330): ... 17 more09-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 more09-07 15:52:52.861: I/ActivityManager(61): displayed com.example.sqlservercall/.MainActivity: +1s503ms09-07 15:53:00.081: W/ActivityManager(61): finishReceiver called but no pending broadcasts
解决方法:
我已成功完成了与JTDC AndroID到sqlServer的连接.
谁想要与AndroID直接连接sqlServer应该首先记住“这根本不是安全的方式”.
我遵循的步骤:
>请记住,JDBC会产生SSL问题,JTDC驱动程序是开源且安全的.所以使用JTDC驱动程序.
>使用JTDC驱动程序时,请确保使用正确的版本.即JTDC驱动程序版本应与java版本匹配或匹配.我用过jtds-1.2.5-dist
你可以从网上的任何地方得到它.
>将Jar文件粘贴到libs文件夹.
>转到Java构建路径(右键单击项目>属性)在库中>添加Externl罐子.导入Jar文件.
>按顺序和出口>检查jtds jar如下:
(你在pic中观看的其他罐子没用)
6您必须设置sqlServer登录身份验证(sqlServer的windows身份验证)屏幕截图如下:
7按如下方式配置sqlServer:
启用TCP / IP和VIA连接.
在TCP / IP属性中将端口更改为1433
8然后,您必须将连接URL设置为:
url=”jdbc:jtds:sqlserver://10.0.2.2:1433;instancename=14GRAFICAli\MSsqlSERVER2008;Databasename=”+DBname+”;integratedSecurity=true;user=”+Username+”;password=”+Password;
9从这里你可以简单地按照我给你的代码:
public class gaConnection {String url ="";Connection conn=null;Statement statement=null;ResultSet resultSet=null; public voID setConnection(String DBname,String Username,String Password) { try { Class.forname("net.sourceforge.jtds.jdbc.Driver").newInstance(); url ="jdbc:jtds:sqlserver://10.0.2.2:1433;instancename=14GRAFICAli\MSsqlSERVER2008;Databasename="+DBname+";integratedSecurity=true;user="+Username+";password="+Password; conn =DriverManager.getConnection(url); } catch (Exception e) { e.printstacktrace(); } } public String showResult() { String strResult=""; try { statement=conn.createStatement(); resultSet=statement.executequery("select * from UserMaster"); while(resultSet.next()){ strResult = strResult + " name : "+resultSet.getString(1)+" Sirname : "+resultSet.getString(2) + "\n"; } } catch (Exception e) { e.printstacktrace(); } return strResult; }}
10我已经创建了Class For Connection并将其实例化如下:
public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); TextVIEw tvData=(TextVIEw)findVIEwByID(R.ID.tvSelectedData); String DBname="AndroIDDB"; String Username="sa"; String Password="ok"; try {gaConnection con=new gaConnection(); con.setConnection(DBname,Username,Password); tvData.setText(con.showResult()); } catch (Exception e) { e.printstacktrace(); tvData.setText(e.getMessage()); } } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
总结 以上是内存溢出为你收集整理的java – 虽然在项目中添加了特定的jar但没有找到类(没有webservice的android到sqlserver)全部内容,希望文章能够帮你解决java – 虽然在项目中添加了特定的jar但没有找到类(没有webservice的android到sqlserver)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)