
我正在编写一个将连接到MySQL服务器的Android应用程序.现在,我正在使用http://localhost:3306/通过XAMPP在我的计算机上测试MysqL服务器.下面的代码在严格作为Java应用程序进行测试时可以正常工作.
import java.sql.*;public class MysqL{ public static voID main(String[] args) { System.out.println("MysqL Connect Example."); Connection conn = null; String url = "jdbc:MysqL://localhost:3306/"; String dbname = "database"; String driver = "com.MysqL.jdbc.Driver"; String username = "root"; String password = ""; try { Class.forname(driver).newInstance(); conn = DriverManager.getConnection(url+dbname,username,password); System.out.println("Connected to the database"); conn.close(); System.out.println("disconnected from database"); } catch (Exception e) { e.printstacktrace(); } }}但是,当我将其添加到AndroID应用程序时,出现Exception e错误.
// interact with MysqL!!! private VIEw.OnClickListener onSendRequest = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { EditText username = (EditText) findVIEwByID(R.ID.Huusername); // String un = " " + username.getText().toString() + " "; System.out.println("MysqL Connect Example."); Connection conn = null; String url = "jdbc:MysqL://localhost:3306/"; String dbname = "database"; String driver = "com.MysqL.jdbc.Driver"; String username = "root"; String password = ""; try { Class.forname(driver).newInstance(); conn = DriverManager.getConnection(url+dbname,username,password); Toast.makeText(getBaseContext(), "Connected to the database.", Toast.LENGTH_LONG) .show(); conn.close(); Toast.makeText(getBaseContext(), "disconnected form the database.", Toast.LENGTH_LONG) .show(); } catch (Exception e) { Toast.makeText(getBaseContext(), "Exception e.", Toast.LENGTH_LONG) .show(); e.printstacktrace(); } } };在编译AndroID应用程序时,我在控制台中注意到以下语句:
[2011-01-26 16:05:54 - hookup]: Dxwarning: Ignoring InnerClasses attribute for an anonymous inner class(com.MysqL.jdbc.interceptors.ResultSetScannerInterceptor) that doesn't come with anassociated EnclosingMethod attribute. This class was probably produced by acompiler that dID not target the modern .class file format. The recommendedsolution is to recompile the class from source, using an up-to-date compilerand without specifying any "-target" type options. The consequence of ignoringthis warning is that reflective operations on this class will incorrectlyindicate that it is *not* an inner class.我认为该语句与我得到的异常有关.有没有人与MysqL和AndroID合作过,可以引导我朝正确的方向发展?
谢谢
解决方法:
使用JDBC连接器mysql-connector-java-3.0.17-ga-bin.jar,目前是唯一可以使用的连接器.
还要检查您的代码,您需要为应用程序的每个活动实现一个连接池,只需添加一个私有的“ Connection”变量并在您的OnCreate方法中将其初始化,例如con = DriveManager.getConnection ……,然后使用该变量每次您需要访问MysqL时都使用private var.
总结以上是内存溢出为你收集整理的Java-使用com.mysql.jdbc.Driver的Android MySQL全部内容,希望文章能够帮你解决Java-使用com.mysql.jdbc.Driver的Android MySQL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)