我正在尝试在学习Android时使用MySQL,但是未发现此类发生异常。我已将MySQL连接器jar文件导入到我的项目中,并进行了设置。我用谷歌搜索了这个问题,但只得到了同一个问题的tomcat解决方案,但我仍然不知道如何在Android中解决这个问题。
private class GetData extends AsyncTask<String,String,String> {
String msg = "";
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://"+
DBStrings.DATABASE_URL +"/"+
DBStrings.DATABASE_NAME;
@Override
protected void onPreExecute(){
progressTextView.setText("Connecting to database");
}
@Override
protected String doInBackground(String... strings) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,DBStrings.USERNAME,DBStrings.PASSWORD);
stmt = conn.createStatement();
String sql = "SELECT * FROM medicine";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
Integer ID = rs.getInt("ID");
String name = rs.getString("Name");
Integer date = rs.getInt("Date");
medID.add(ID);
names.add(name);
medDate.add(date);
}
msg = "complete!";
rs.close();
stmt.close();
conn.close();
}catch (SQLException connERROR)
{
msg = "An exception was thrown for JDBC";
connERROR.printStackTrace();
}catch (ClassNotFoundException classERROR)
{
msg = "Class not found exception";
classERROR.printStackTrace();
}finally {
}
return null;
}
}
答案 0 :(得分:1)
不要在Android设备上使用JDBC,只需使用J2EE servlet(如果要在JAVA中进行编码,这是最简单的解决方案),然后通过HTTP从android调用这些servlet(您可以使用Retrofit库进行这些调用{{3 }}。