使用异步任务连接到我的 PostgreSQL 数据库时出现问题。当使用正确的数据库设置(pg_host、pg_user 等)尝试它时,它可以工作。问题是当尝试使用错误设置时,Activity 将关闭,因为 getConnection() 方法具有错误参数。但我希望用户手动将设置放入 settings_activity 中,以便他可以更改他们自己的数据库的设置,如果存储了错误的设置,则应该出现警告 toast 并提示用户更改设置。Activity 每次都会中断,我在做什么错?非常感谢。
代码:
1 protected void onCreate(Bundle savedInstanceState) {
2 super.onCreate(savedInstanceState);
3 setContentView(R.layout.main);
4
5 getPrefs();
6 //.
7 //.
8 //.
9 new FetchSQL().execute();
10 }
11
12 public void getPrefs() {
13 SharedPreferences prefs = PreferenceManager
14 .getDefaultSharedPreferences(this);
15
16 pg_host = prefs.getString("host", "HOST");
17 pg_port = prefs.getString("port", "PORT");
18 pg_database = prefs.getString("database", "DATABASE");
19 pg_user = prefs.getString("user", "USER");
20 pg_pw = prefs.getString("password", "PW");
21 }
22
23
24 private class FetchSQL extends AsyncTask<Void,Void,String> {
25
26 @Override
27 protected String doInBackground(Void... params) {
28 String retval="";
29 try {
30 Class.forName("org.postgresql.Driver");
31 } catch (ClassNotFoundException e) {
32 e.printStackTrace();
33 retval = e.toString();
34 Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_LONG).show();
35 }
36 String url = "jdbc:postgresql://"+pg_host+":"+pg_port+"/"+pg_database+"";
37 Connection conn;
38 try {
39 DriverManager.setLoginTimeout(5);
40 conn = DriverManager.getConnection(url, pg_user, pg_pw);
41 Statement st = conn.createStatement();
42 String sql;
43 sql = //SQL Query
44 ResultSet rs = st.executeQuery(sql);
45 while(rs.next()) {
46
47 //e.g var .. = rs.getString("...");
48 }
49 rs.close();
50 st.close();
51 conn.close();
52 }
53 catch (SQLException e) {
54 e.printStackTrace();
55 retval = e.toString();
56 Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_LONG).show();
57 }
58 return retval;
59
60 }
61
62 @Override
63 protected void onPostExecute(String value) {
64 //Show Data from Query in textView
65 }
日志猫:
102-14 12:21:18.707: W/System.err(2128): java.sql.SQLException: No suitable driver
202-14 12:21:18.767: W/System.err(2128): at java.sql.DriverManager.getConnection(DriverManager.java:182)
302-14 12:21:18.877: W/System.err(2128): at java.sql.DriverManager.getConnection(DriverManager.java:209)
402-14 12:21:18.928: W/System.err(2128): at MainActivity$FetchSQL.doInBackground(MainActivity.java:318)
502-14 12:21:18.987: W/System.err(2128): at .MainActivity$FetchSQL.doInBackground(MainActivity.java:1)
602-14 12:21:19.047: W/System.err(2128): at android.os.AsyncTask$2.call(AsyncTask.java:287)
702-14 12:21:19.099: W/System.err(2128): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
802-14 12:21:19.157: W/System.err(2128): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
902-14 12:21:19.217: W/System.err(2128): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
1002-14 12:21:19.267: W/System.err(2128): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
1102-14 12:21:19.437: W/System.err(2128): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
1202-14 12:21:19.467: W/System.err(2128): at java.lang.Thread.run(Thread.java:856)