Android examples for Database:SQL Query
test SQL query on SQLServer database
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import android.os.Bundle; import android.os.Message; import android.widget.TextView; public class Main{ final static String user = "sa"; final static String pwd = ""; final static String db = "databaseName"; public static String testSQL() { String result = "1 - 2\n"; try {//from w w w. j a v a 2 s . co m Connection conn = getSQLConnection(); String sql = "select * from ProblemData"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { String s1 = rs.getString("TrainNum"); String s2 = rs.getString("SubmitDate"); 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 Connection getSQLConnection() { Connection con = null; try { if (!LoginActivity.TestConnect()) return null; Class.forName("net.sourceforge.jtds.jdbc.Driver"); DriverManager.setLoginTimeout(1); con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + LoginActivity.strServerIP + ":1433/" + db + ";charset=utf8", user, pwd); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return con; } }