问题描述
- 在android中用jdbc连接数据库出现的问题,求大神帮忙?
-
05-21 12:45:01.219: E/TAG(13965): js dfnoej
05-21 12:45:01.439: E/AndroidRuntime(13965): FATAL EXCEPTION: Thread-127
05-21 12:45:01.439: E/AndroidRuntime(13965): java.lang.ExceptionInInitializerError
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:776)
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:337)
05-21 12:45:01.439: E/AndroidRuntime(13965): at java.sql.DriverManager.getConnection(DriverManager.java:175)
05-21 12:45:01.439: E/AndroidRuntime(13965): at java.sql.DriverManager.getConnection(DriverManager.java:209)
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.bn.day18.LandMainActivity$1.run(LandMainActivity.java:35)
05-21 12:45:01.439: E/AndroidRuntime(13965): at java.lang.Thread.run(Thread.java:856)
05-21 12:45:01.439: E/AndroidRuntime(13965): Caused by: java.lang.ExceptionInInitializerError
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.mysql.jdbc.Util.stackTraceToString(Util.java:355)
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.mysql.jdbc.Util.(Util.java:120)
05-21 12:45:01.439: E/AndroidRuntime(13965): ... 6 more
05-21 12:45:01.439: E/AndroidRuntime(13965): Caused by: java.lang.RuntimeException: Can't load resource bundle due to underlying exception java.util.MissingResourceException: Can't find resource for bundle 'com.mysql.jdbc.LocalizedErrorMessages_zh_CN', key ''
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.mysql.jdbc.Messages.(Messages.java:61)
05-21 12:45:01.439: E/AndroidRuntime(13965): ... 8 more
05-21 12:45:01.439: E/AndroidRuntime(13965): Caused by: java.util.MissingResourceException: Can't find resource for bundle 'com.mysql.jdbc.LocalizedErrorMessages_zh_CN', key ''
05-21 12:45:01.439: E/AndroidRuntime(13965): at java.util.ResourceBundle.missingResourceException(ResourceBundle.java:239)
05-21 12:45:01.439: E/AndroidRuntime(13965): at java.util.ResourceBundle.getBundle(ResourceBundle.java:231)
05-21 12:45:01.439: E/AndroidRuntime(13965): at java.util.ResourceBundle.getBundle(ResourceBundle.java:140)
05-21 12:45:01.439: E/AndroidRuntime(13965): at com.mysql.jdbc.Messages.(Messages.java:59)
05-21 12:45:01.439: E/AndroidRuntime(13965): ... 8 more
解决方案
jdbc驱动没有找到。另外,android是客户端系统,应该用sqlite,不应该用mysql,如果用mysql,也是放在服务器上,通过web service访问,而不是直接访问。
解决方案二:
我们老师就让我们用jdbc来访问MySQL数据库的内容,来做一个购物 的,我们还没学sqlite, 也没学web seriver,之前连接数据库用的是Java,可以连上,
但是换成Android就死活连不上了,下面是我的在Android中写的代码,麻烦你看看
new Thread(new Runnable() {
@Override
public void run() {
try {
Class.forName("com.mysql.jdbc.Driver");
Log.e("TAG", "js dfnoej");
connection = java.sql.DriverManager.getConnection("jdbc:mysql://196.168.1.103:3306/muyuan","root","root");
Log.e("TAG", "haha");
String sql = "SELECT *FROM goods" ;
PreparedStatement prepareStatement = connection.prepareStatement(sql);
ResultSet resultSet = prepareStatement.executeQuery();
while (resultSet.next()) {
list.add(sql);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("sql异常");
}
}
}).start();