android jdbc:jtds连接问题 懂得看看谢谢
搞了好久弄不出,麻烦帮忙看一看,谢谢
下面贴了代码,
问题:
1、con = DriverManager.getConnection(url,"papi","xy123456");//con不能输出显示
2、Statement stmt = con.createStatement();//这句后面不能运行了。。
package com.example.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView00;
private TextView textView01;
private TextView textView02;
private TextView textView03;
private String str03="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView00 = (TextView)this.findViewById(R.id.TextView00);
textView01 = (TextView)this.findViewById(R.id.TextView01);
textView02 = (TextView)this.findViewById(R.id.TextView02);
textView03 = (TextView)this.findViewById(R.id.TextView03);
MyRunnable mr1 = new MyRunnable();
Thread t1 = new Thread(mr1);
t1.start();
}
class MyRunnable implements Runnable {
@Override
public void run() {
Connection con = null;
try {
String driver="net.sourceforge.jtds.jdbc.Driver";
String url="jdbc:jtds:sqlserver://115.198.192.133:3322/cat";
Class.forName(driver).newInstance();
textView00.setText(" 连接开始。。。。。。");
long starttime = System.currentTimeMillis();
Log.e("TAG", "------------->>before getConnection ");
con = DriverManager.getConnection(url,"papi","xy123456");
textView03.setText("111" + con);
Log.e("TAG", "------------->>after getConnection ");
long endtime = System.currentTimeMillis();
textView01.setText(" 连接成功 !Lost time:" + (endtime - starttime) * 10 + "秒");
Log.e("TAG", "------------->>after 连接成功 ");
con.close();
} catch(Exception e) {
e.printStackTrace();
} finally{
try {
if(null != con){
con.close();
Log.e("TAG", "------------->>con close ");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
testConnection(con);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void testConnection(Connection con) throws java.sql.SQLException {
try {
String sql = "SELECT * FROM info";//查询表名为“table_test”的所有内容
Log.e("TAG", "------------->>after string sql ");
Statement stmt = con.createStatement();//创建Statement
Log.e("TAG", "------------->>before ResultSet ");
ResultSet rs = stmt.executeQuery(sql);//ResultSet类似Cursor
while (rs.next()) {//<code>ResultSet</code>最初指向第一行
str03 += rs.getString("school");
textView02.setText(str03);
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage().toString());
} finally {
if (con != null)
try {
con.close();
} catch (SQLException e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
下面是LogCAT:
05-02 08:23:05.503: E/TAG(1747): ------------->>before getConnection
05-02 08:23:07.753: I/Choreographer(1747): Skipped 140 frames! The application may be doing too much work on its main thread.
05-02 08:23:08.283: D/gralloc_goldfish(1747): Emulator without GPU emulation detected.
05-02 08:23:11.523: D/dalvikvm(1747): GC_FOR_ALLOC freed 247K, 12% free 2768K/3136K, paused 131ms, total 143ms
05-02 08:23:14.323: W/System.err(1747): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-02 08:23:14.533: W/System.err(1747): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
05-02 08:23:14.564: W/System.err(1747): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
05-02 08:23:14.564: W/System.err(1747): at android.view.View.requestLayout(View.java:15792)
05-02 08:23:14.623: W/System.err(1747): at android.view.View.requestLayout(View.java:15792)
05-02 08:23:14.623: W/System.err(1747): at android.view.View.requestLayout(View.java:15792)
05-02 08:23:14.623: W/System.err(1747): at android.view.View.requestLayout(View.java:15792)
05-02 08:23:14.623: W/System.err(1747): at android.view.View.requestLayout(View.java:15792)
05-02 08:23:14.623: W/System.err(1747): at android.widget.TextView.checkForRelayout(TextView.java:6510)
05-02 08:23:14.643: W/System.err(1747): at android.widget.TextView.setText(TextView.java:3764)
05-02 08:23:14.643: W/System.err(1747): at android.widget.TextView.setText(TextView.java:3622)
05-02 08:23:14.643: W/System.err(1747): at android.widget.TextView.setText(TextView.java:3597)
05-02 08:23:14.657: W/System.err(1747): at com.example.test.MainActivity$MyRunnable.run(MainActivity.java:47)
05-02 08:23:14.676: W/System.err(1747): at java.lang.Thread.run(Thread.java:841)
05-02 08:23:14.684: E/TAG(1747): ------------->>con close
05-02 08:23:14.694: E/TAG(1747): ------------->>after string sql
05-02 08:23:14.702: I/System.out(1747): Invalid state, the Connection object is closed.
求大神解决解决!!