程式碼OK
執行後,一大堆錯誤,連錯誤是什麼都看不懂
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectTodatabase();
}
public void connectTodatabase()
{
TextView tv1=(TextView)findViewById(R.id.tv1);
String url = "jdbc:jtds:sqlserver://192.168.10.5:1433;DatabaseName=Northwind";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String userName = "sa";
String password = "12345678";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
// Establish the connection.
Class.forName(driver);
con = DriverManager.getConnection(url, userName, password);
// Create and execute an SQL statement that returns some data.
// String SQL = "SELECT ShipCity FROM Orders where CustomerID='ALFKI' and OrderID=10643";
String SQL = "SELECT sum(Freight) as 總金額 FROM Orders where CustomerID='ALFKI'";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
tv1.setText(rs.getString(1));
}
}
catch(Exception ex)
{
tv1.setText(ex.getMessage().toString());
}
}
}
其中一個錯誤訊息
--------- beginning of crash
08-16 12:44:47.454 17474-17474/com.pangu.sqlserver3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pangu.sqlserver3, PID: 17474
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pangu.sqlserver3/com.pangu.sqlserver3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' 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.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
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 virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.pangu.sqlserver3.MainActivity.connectTodatabase(MainActivity.java:50)
at com.pangu.sqlserver3.MainActivity.onCreate(MainActivity.java:16)
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.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
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)