Guest User

Untitled

a guest
Jan 6th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package com.test1;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7.  
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12. public class Test1Activity extends Activity {
  13. /** Called when the activity is first created. */
  14. String str="new";
  15. static ResultSet rs;
  16. static PreparedStatement st;
  17. static Connection con;
  18.  
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. final TextView tv=(TextView)findViewById(R.id.user);
  24.  
  25. try
  26. {
  27. Class.forName("com.mysql.jdbc.Driver");
  28. con=DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/example","root","");
  29. st=con.prepareStatement("select * from country where id=1");
  30. rs=st.executeQuery();
  31. while(rs.next())
  32. {
  33. str=rs.getString(2);
  34.  
  35.  
  36. }
  37.  
  38.  
  39. tv.setText(str);
  40. setContentView(tv);
  41. }
  42. catch(Exception e)
  43. {
  44. tv.setText(str);
  45. }
  46. }
  47. }
  48.  
  49. java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
  50. Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
  51. Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom
  52.  
  53. public void mysql() {
  54.  
  55. try {
  56. Class.forName("com.mysql.jdbc.Driver");
  57. } catch (ClassNotFoundException e) {
  58. e.printStackTrace();
  59.  
  60. }
  61.  
  62. thrd1 = new Thread(new Runnable() {
  63. public void run() {
  64. while (!Thread.interrupted()) {
  65. try {
  66. Thread.sleep(100);
  67. } catch (InterruptedException e1) {
  68.  
  69. }
  70. if (con == null) {
  71. try {
  72. con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345");
  73.  
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. con = null;
  77. }
  78.  
  79. if ((thrd2 != null) && (!thrd2.isAlive()))
  80. thrd2.start();
  81.  
  82. }
  83. }
  84.  
  85. }
  86. });
  87. if ((thrd1 != null) && (!thrd1.isAlive())) thrd1.start();
  88.  
  89. thrd2 = new Thread(new Runnable() {
  90. public void run() {
  91. while (!Thread.interrupted()) {
  92.  
  93. if (con != null) {
  94. try {
  95. // con = DriverManager.getConnection("jdbc:mysql://192.168.1.45:3306/deneme", "ali", "12345");
  96. Statement st = con.createStatement();
  97. String ali = "'fff'";
  98. st.execute("INSERT INTO deneme (name) VALUES(" + ali + ")");
  99. // ResultSet rs = st.executeQuery("select * from deneme");
  100. // ResultSetMetaData rsmd = rs.getMetaData();
  101. // String result = new String();
  102.  
  103.  
  104. // while (rs.next()) {
  105. // result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "n";
  106. // result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "n";
  107.  
  108.  
  109. // }
  110.  
  111. } catch (SQLException e) {
  112. e.printStackTrace();
  113. con = null;
  114. }
  115.  
  116. try {
  117. Thread.sleep(10);
  118. } catch (InterruptedException e) {
  119. e.printStackTrace();
  120. }
  121. } else {
  122. try {
  123. Thread.sleep(300);
  124. } catch (InterruptedException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. }
  129. }
  130.  
  131. });
  132.  
  133.  
  134. }
  135.  
  136. public void testDB() {
  137. TextView tv = (TextView) this.findViewById(R.id.tv_data);
  138. try {
  139.  
  140. Class.forName("com.mysql.jdbc.Driver");
  141.  
  142. // perfect
  143.  
  144. // localhost
  145.  
  146. /*
  147. * Connection con = DriverManager .getConnection(
  148. * "jdbc:mysql://192.168.1.5:3306/databasename?user=root&password=123"
  149. * );
  150. */
  151.  
  152. // online testing
  153.  
  154. Connection con = DriverManager
  155. .getConnection("jdbc:mysql://173.5.128.104:3306/vokyak_heyou?user=viowryk_hiweser&password=123");
  156.  
  157. String result = "Database connection successn";
  158. Statement st = con.createStatement();
  159.  
  160. ResultSet rs = st.executeQuery("select * from tablename ");
  161. ResultSetMetaData rsmd = rs.getMetaData();
  162.  
  163. while (rs.next()) {
  164.  
  165. result += rsmd.getColumnName(1) + ": " + rs.getString(1) + "n";
  166.  
  167. }
  168. tv.setText(result);
  169. } catch (Exception e) {
  170. e.printStackTrace();
  171. tv.setText(e.toString());
  172. }
  173.  
  174. }
  175.  
  176. targetSdkVersion 8
Add Comment
Please, Sign In to add comment