现在在弄安卓手机端通过SQL命令访问电脑上的SQL数据库(通过网络)。基本代码已写,但是老是出现问题。希望哪个大神能帮忙解决。 希望具体点谢谢。以下是我的代码和出现的错误。
import java.sql.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends Activity {
TextView message = null;
EditText sql_command_et = null;
java.sql.Connection ct = null;
PreparedStatement ps=null;
ResultSet rs=null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
message = (TextView) findViewById(R.id.message);
Button btn = (Button) findViewById(R.id.btn);
sql_command_et = (EditText) findViewById(R.id.sql_command_et);
message.setText("");// 文本框
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); // 加载MYSQL JDBC驱动程序
ct = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.1.101:1433;databaseName=Heros","sa","sa");


// 连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
         
} catch (SQLException ee) {
ee.printStackTrace();
} catch (Exception e) {
System.out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { String sql = sql_command_et.getText().toString().trim();
try {
PreparedStatement ps =  ct.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
message.append(rs.getString("heroName")+ "\n");

}
rs.close();
ps.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
// message.append(DBManager.selectPwd("10001"));
}
}); }
}
以下是在模拟器上运行时出现的问题。程序没报错。但是在logcat里出现了很多异样。点击查询数据库会出现以下东西:
安桌数据库二维码连接数据库JAVA