我很沮喪..... Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance()
不斷給出錯誤。奇怪,但在下面的代碼中,MainActivity對gaConnecion.setConnection
的調用工作正常!但是從ResultsActivity失敗並出現上述錯誤:在Android中加載jtds驅動程序時出現NoClassDefFoundError
gaConnection.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.widget.Toast;
public class gaConnection
{
String url ="";
Connection conn=null;
Statement statement=null;
ResultSet resultSet=null;
public void setConnection(String DBName,String UserName,String Password, Context context)
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
url ="jdbc:jtds:sqlserver://212.150.9.60:1433;instanceName=MSSQLServer;DatabaseName="+DBName+";integratedSecurity=true;user="+UserName+";password="+Password;
conn =DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public String showResult()
{
String strResult="";
try
{
statement=conn.createStatement();
resultSet=statement.executeQuery("select * from vehicles where number='4195666'");
while(resultSet.next()){
strResult = strResult + " Name : "+resultSet.getString(1)+" SirName : "+resultSet.getString(2) + "\n";
}
}
catch (Exception e) {
e.printStackTrace();
}
return strResult;
}
public void storeInfo(String text) {
String dateStamp = new SimpleDateFormat("ddMMyy").format(new Date());
String hourStamp = new SimpleDateFormat("HHmmss").format(new Date());
try
{
statement=conn.createStatement();
resultSet=statement.executeQuery("insert into vehicles(number, date, time) values('" + text + "', '" + dateStamp + "', '" + hourStamp + "')");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
ResultsActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class ResultsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
getResultsFromSQL();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.results, menu);
return true;
}
public void getResultsFromSQL() {
String DBName="hofyam";
String UserName="sa";
String Password="sadmin";
TextView tvData=(TextView)findViewById(R.id.textSQLResults);
try {gaConnection c=new gaConnection();
c.setConnection(DBName,UserName,Password, getApplicationContext());
tvData.setText(c.showResult());
} catch (Exception e) {
e.printStackTrace();
tvData.setText(e.getMessage());
}
}
}
的logcat:
01-13 10:43:26.444: V/Provider/Setting(31537): invalidate [system]: current 171 != cached 0
01-13 10:43:26.447: V/Provider/Setting(31537): from db cache, name = sound_effects_enabled value = 0
01-13 10:43:26.448: I/fengjun1010(31537): -----startActivityForResult,intent=Intent { cmp=com.example.hofyam/.ResultsActivity (has extras) }
01-13 10:43:26.449: I/fengjun1010(31537): -----agoldPhoneStateListener-----
01-13 10:43:26.449: I/fengjun1010(31537): ----------mPhoneListenerFlag=true
01-13 10:43:26.449: I/fengjun1010(31537): ------------agoldChangeGoodleStoreApp---------
01-13 10:43:26.449: I/fengjun1010(31537): -------------------mChangeFlag=false
01-13 10:43:26.449: I/fengjun1010(31537): ----startActivity---,intent=Intent { cmp=com.example.hofyam/.ResultsActivity (has extras) }
01-13 10:43:26.450: I/fengjun1010(31537): ---ComponentInfo{com.example.hofyam/com.example.hofyam.ResultsActivity}
01-13 10:43:26.478: D/ActivityThread(31537): ACT-AM_ON_PAUSE_CALLED ActivityRecord{41623298 [email protected] {com.example.hofyam/com.example.hofyam.MainActivity}}
01-13 10:43:26.504: D/ActivityThread(31537): ACT-PAUSE_ACTIVITY handled : 1/[email protected]
01-13 10:43:26.508: I/fengjun1010(31537): -----agoldPhoneStateListener-----
01-13 10:43:26.508: I/fengjun1010(31537): ----------mPhoneListenerFlag=true
01-13 10:43:26.630: D/NativeCrypto(31537): NativeCrypto_EVP_VerifyInit ctx=0x52c0c020
01-13 10:43:26.631: D/NativeCrypto(31537): NativeCrypto_EVP_VerifyInit algorithmChars=RSA-SHA1
01-13 10:43:26.729: I/System.out(31537): [CDS]connect[/212.150.9.60:1433] tm:90
01-13 10:43:26.730: W/System.err(31537): android.os.NetworkOnMainThreadException
01-13 10:43:26.758: W/System.err(31537): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
01-13 10:43:26.758: W/System.err(31537): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-13 10:43:26.758: W/System.err(31537): at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
01-13 10:43:26.758: W/System.err(31537): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-13 10:43:26.759: W/System.err(31537): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-13 10:43:26.759: W/System.err(31537): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-13 10:43:26.759: W/System.err(31537): at java.net.Socket.startupSocket(Socket.java:575)
01-13 10:43:26.760: W/System.err(31537): at java.net.Socket.tryAllAddresses(Socket.java:130)
01-13 10:43:26.760: W/System.err(31537): at java.net.Socket.<init>(Socket.java:180)
01-13 10:43:26.761: W/System.err(31537): at java.net.Socket.<init>(Socket.java:152)
01-13 10:43:26.761: W/System.err(31537): at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:252)
01-13 10:43:26.762: W/System.err(31537): at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:297)
01-13 10:43:26.762: W/System.err(31537): at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:181)
01-13 10:43:26.762: W/System.err(31537): at java.sql.DriverManager.getConnection(DriverManager.java:175)
01-13 10:43:26.763: W/System.err(31537): at java.sql.DriverManager.getConnection(DriverManager.java:140)
01-13 10:43:26.763: W/System.err(31537): at com.example.hofyam.gaConnection.setConnection(gaConnection.java:24)
01-13 10:43:26.764: W/System.err(31537): at com.example.hofyam.ResultsActivity.getResultsFromSQL(ResultsActivity.java:32)
01-13 10:43:26.764: W/System.err(31537): at com.example.hofyam.ResultsActivity.onCreate(ResultsActivity.java:15)
01-13 10:43:26.765: W/System.err(31537): at android.app.Activity.performCreate(Activity.java:5158)
01-13 10:43:26.765: W/System.err(31537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-13 10:43:26.766: W/System.err(31537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
01-13 10:43:26.767: W/System.err(31537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
01-13 10:43:26.768: W/System.err(31537): at android.app.ActivityThread.access$600(ActivityThread.java:149)
01-13 10:43:26.769: W/System.err(31537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
01-13 10:43:26.769: W/System.err(31537): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 10:43:26.770: W/System.err(31537): at android.os.Looper.loop(Looper.java:153)
01-13 10:43:26.770: W/System.err(31537): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-13 10:43:26.770: W/System.err(31537): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 10:43:26.771: W/System.err(31537): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 10:43:26.772: W/System.err(31537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-13 10:43:26.772: W/System.err(31537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-13 10:43:26.772: W/System.err(31537): at dalvik.system.NativeStart.main(Native Method)
01-13 10:43:26.816: W/System.err(31537): java.lang.NullPointerException
01-13 10:43:26.817: W/System.err(31537): at com.example.hofyam.gaConnection.showResult(gaConnection.java:36)
01-13 10:43:26.817: W/System.err(31537): at com.example.hofyam.ResultsActivity.getResultsFromSQL(ResultsActivity.java:33)
01-13 10:43:26.817: W/System.err(31537): at com.example.hofyam.ResultsActivity.onCreate(ResultsActivity.java:15)
01-13 10:43:26.817: W/System.err(31537): at android.app.Activity.performCreate(Activity.java:5158)
01-13 10:43:26.817: W/System.err(31537): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-13 10:43:26.817: W/System.err(31537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
01-13 10:43:26.818: W/System.err(31537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
01-13 10:43:26.818: W/System.err(31537): at android.app.ActivityThread.access$600(ActivityThread.java:149)
01-13 10:43:26.818: W/System.err(31537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
01-13 10:43:26.818: W/System.err(31537): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 10:43:26.818: W/System.err(31537): at android.os.Looper.loop(Looper.java:153)
01-13 10:43:26.818: W/System.err(31537): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-13 10:43:26.818: W/System.err(31537): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 10:43:26.818: W/System.err(31537): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 10:43:26.818: W/System.err(31537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-13 10:43:26.819: W/System.err(31537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-13 10:43:26.819: W/System.err(31537): at dalvik.system.NativeStart.main(Native Method)
01-13 10:43:26.819: D/ActivityThread(31537): ACT-AM_ON_RESUME_CALLED ActivityRecord{4166e1d8 [email protected] {com.example.hofyam/com.example.hofyam.ResultsActivity}}
01-13 10:43:26.822: D/ActivityThread(31537): ACT-LAUNCH_ACTIVITY handled : 0/ActivityRecord{4166e1d8 [email protected] {com.example.hofyam/com.example.hofyam.ResultsActivity}}
01-13 10:43:26.823: V/InputMethodManager(31537): focusOut: [email protected] [email protected] winFocus=false
01-13 10:43:26.841: I/SurfaceTextureClient(31537): [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
01-13 10:43:26.872: I/SurfaceTextureClient(31537): [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
01-13 10:43:26.876: V/InputMethodManager(31537): onWindowFocus: null softInputMode=288 first=true flags=#1810100
01-13 10:43:26.876: V/InputMethodManager(31537): Starting input: [email protected]f280
01-13 10:43:26.876: V/InputMethodManager(31537): Starting input: [email protected] ic=null
01-13 10:43:26.877: V/InputMethodManager(31537): START INPUT: [email protected] ic=null [email protected] controlFlags=#104
01-13 10:43:26.879: V/InputMethodManager(31537): Starting input: Bind result=InputBindResult{[email protected]0 com.android.inputmethod.latin/.LatinIME #1581}
01-13 10:43:26.934: D/OpenGLRenderer(31537): Flushing caches (mode 0)
01-13 10:43:26.960: I/SurfaceTextureClient(31537): [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
01-13 10:43:26.972: D/ActivityThread(31537): ACT-STOP_ACTIVITY_HIDE handled : 0/[email protected]
我試過幾個JTDS驅動程序版本:1.3.1,1.2,1.2.5,1.2.7,似乎沒有什麼能解決問題。 感謝您的幫助。
你能顯示logcat錯誤嗎? – blitzen12
發佈了logcat。謝謝 – Kfir