我试图打印存储在mysql数据库中的数据。
数据库名称=“ android”
端口号=“ 3006”
sql =“ select * from tablename”;
private class Data1 extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids) {
Connection con= null;
String username="root";
String password="";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/android",username,password);
PreparedStatement stmt=con.prepareStatement("select * from login");
ResultSet rs=stmt.executeQuery();
while (rs.next()){
System.out.println(rs.getString(1));
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
System.out.println("ok");
}
return null;
}
}
>E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
>Process: com.example.login, PID: 10158
>java.lang.RuntimeException: An error occurred while executing doInBackground()