class DBUtil {
private static Connection getSQLConnection(String ip, String user, String pwd, String db)
{
Connection con = null;
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Log.d("驅動", "getSQLConnection: 成功");
con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db, user, pwd);
Log.d("資料庫連接", "getSQLConnection: 成功");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
public static String QuerySQL()
{
String result = "";
try
{
Connection conn = getSQLConnection("192.168.0.102", "sa", "88888888", "parking");
Log.d("parking", "getSQLConnection: 成功");
String sql = "select top 2 * from parking";
Statement stmt = conn.createStatement();//錯誤代碼處

ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
{
String s1 = rs.getString("Carid");
String s2 = rs.getString("Time");
result += s1 + " - " + s2 + "\n";
System.out.println(s1 + " - " + s2);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
result += "例外!" + e.getMessage();
}
return result;
}
public static void main(String[] args)
{
QuerySQL();
}
}
uj5u.com熱心網友回復:
球球大佬指點一下轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/195660.html
標籤:疑難問題
下一篇:有沒有大佬知道SQLServer 鏈接oracle 資料超過99條就會報這個錯嗎?但是99條以下資料就可以查出來。跪求大佬指導
