我试图将我的android应用程序连接到mssql数据库。但当我尝试连接时,我遇到了一个异常。我已经在调试模式下检查,并从Class.forName
去异常捕获 这里是我的代码。将mssql连接到android应用程序
protected void onStart() {
try
{
// Load the SQLServerDriver class, build the
// connection string, and get a connection
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://xxx\\xxxxx:1433;" +
"database=xxxx;" +
"user=xxxx;" +
"password=*****";
Connection con = DriverManager.getConnection(connectionUrl);
System.out.println("Connected.");
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT COUNT(*) FROM xxxxxx";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText((CharSequence) rs);
// Iterate through the data in the result set and display it.
while (rs.next())
{
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
super.onStart();
}