Код: Выделить всё
public static void main(String[] args) {
Connection conn = null;
try
{
String userName = "Visitor";
String password = "visitor";
String url = "jdbc:mysql://192.168.0.105/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, userName, password);
Statement stat = conn.createStatement();
ResultSet res = stat.executeQuery("SELECT * " +
"FROM `testtable` " +
"where 1 " +
"limit 10; ");
if (res.next()){
System.out.println("Count rows in table: " + res.getInt(1));
}
}catch (Exception e){
System.err.println (e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
Код: Выделить всё
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById (R.id.tv);
Connection conn = null;
try
{
String userName = "Visitor";
String password = "visitor";
String url = "jdbc:mysql://192.168.0.105/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, userName, password);
Statement stat = conn.createStatement();
ResultSet res = stat.executeQuery("SELECT * " +
"FROM `testtable` " +
"where 1 " +
"limit 10; "); // выполняем запрос
if (res.next()){ // переходим к очередной строке ответа от сервера, в данном случае - первой
System.out.println("Count rows in table: " + res.getInt(1));
}
}catch (Exception e){
tv.setText("ERROR \n" + e + "\n\n\n");
System.err.println (e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
}
catch (Exception e) {
tv.append("ERROR \n" + e + "\n\n\n");
e.printStackTrace();
}
}
}
}