我的数据库中有一个名为Meal
的下表。我想将MealType
列加载到数组中,以便我可以使用值填充ListView
。
public class DataAccess {
String ip = "sict-sql.n***.ac.za";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "eMenu";
String un = "cyber";
String password = "********";
public DataAccess(String Ip, String Classs, String Db, String Un, String Password) {
ip = Ip;
classs = Classs;
db = Db;
un = Un;
password = Password;
}
public String getip() {
return ip;
}
public String getclasss() {return classs;}
public String getdb() {return db;}
public String getun() {return un;}
public String getpassword() {return password;}
public void setip(String Ip) {ip = Ip;}
public void setdb(String Db) {db = Db;}
public void setclasss(String Classs) {classs = Classs;}
public void setun(String Un) {un = Un;}
public void setpassword(String Password) {password = Password;}
}
这是我的MainActivity:
public class MainActivity extends ActionBarActivity {
public String[] categories;
public ListView lstCategories;
public String[] categories = {"A"};
DataAccess connectionClass;
String datets;
String call, db, un, passwords;
Connection connect;
ResultSet rs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
connectionClass = new DataAccess();
call = connectionClass.getip();
un = connectionClass.getun();
passwords = connectionClass.getpassword();
db = connectionClass.getdb();
connect = conn(un, passwords, db, call);
GetListViewValue(connect);
}
@SuppressLint("NewApi")
public Connection conn(String _user, String _pass, String _DB,String _server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnURL = "jdbc:sqlserver://" + _server + ";"+ "databaseName=" + _DB + "integratedSecurity=true;;user=" + _user + ";password=" + _pass ;
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Toast.makeText(MainActivity.this, "ERROR: " + se.getMessage(), Toast.LENGTH_LONG).show();
} catch (ClassNotFoundException e) {
Toast.makeText(MainActivity.this, "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
return conn;
}
public void GetListViewValue(Connection connect){
String querycmd = "SELECT * FROM Meal;";
Statement state = null;
try {
state = connect.createStatement();
rs = state.executeQuery(querycmd);
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("A", rs.getString("MealType"));
data.add(datanum);
lstCategories = (ListView) findViewById(R.id.lvCategories);
Typeface font = Typeface.createFromAsset(getAssets(), "Abel-Regular.ttf");
lstCategories.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
value = (String) lstCategories.getItemAtPosition(position);
Toast.makeText(MainActivity.this, value + "? Good choice", Toast.LENGTH_LONG).show();
switch (value) {
default:
Intent to = new Intent(MainActivity.this, MenuActivity.class);
startActivity(to);
break;
}
}
}
);
}
} catch (SQLException e) {
Toast.makeText(MainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (state != null) try { state.close(); } catch(Exception e) {}
if (connect != null) try { connect.close(); } catch(Exception e) {}
}
}
}
这是我的homescreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/loMain">
<LinearLayout
android:orientation="horizontal"
android:layout_width="350dp"
android:layout_height="match_parent"
android:weightSum="1">
<ListView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="@+id/lvCategories" />
</LinearLayout>
</LinearLayout>
Toast给我一个net.sourceforge.jtds.jdbc.Driver
错误,然后应用程序关闭。这是logcat:
07-09 05:53:43.684 2248-2248/siphe.androidapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: siphe.androidapp, PID: 2248
java.lang.RuntimeException: Unable to start activity ComponentInfo{siphe.androidapp/siphe.androidapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
at siphe.androidapp.MainActivity$override.GetListViewValue(MainActivity.java:87)
at siphe.androidapp.MainActivity$override.access$dispatch(MainActivity.java)
at siphe.androidapp.MainActivity.GetListViewValue(MainActivity.java:0)
at siphe.androidapp.MainActivity$override.onCreate(MainActivity.java:57)
at siphe.androidapp.MainActivity$override.access$dispatch(MainActivity.java)
at siphe.androidapp.MainActivity.onCreate(MainActivity.java:0)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我认为这是我安装的驱动程序。请帮忙