我正在做一个测试应用程序直接连接到MS SqlServer 2008,但是我需要一些帮助才能知道我的代码是错误还是我无法直接进行连接。
发生的事情是,我使用TextView只是为了向我显示插入数据库表中的值,但这不会发生,只会出现" Hello World"消息,当我启动应用程序时。
我使用的是jTDS驱动程序1.2.5,但已经使用了1.2.7和1.3.1但它们没有显示任何结果。
请按照下面的代码进行操作。
// Connection with DataBase (SQL SERVER 2008)//
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
String data;
TextView textView;
textView = (TextView)findViewById(R.id.txtData);
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnURL = "jdbc:jtds:sqlserver://192.168.0.169:1433/ANDROID_SQL;username=sa;password=@dm1n102030;instance=MEGACONTROL";
conn = DriverManager.getConnection(ConnURL);
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from usuarios");
resultSet.next();
data = resultSet.getString("nome");
textView.setText(data);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}