Guest User

Untitled

a guest
Jun 4th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.lang.*;
  7.  
  8.  
  9. public class main{
  10. private static void request(){
  11. String url = "jdbc:mysql://myadress:3306/mydatabase";
  12. String user = "user";
  13. String passwd = "passwd";
  14.  
  15. Connection conn = null;
  16. try{
  17. /* Initializing the connection */
  18. conn = DriverManager.getConnection(url, user, passwd);
  19.  
  20. Statement statement = conn.createStatement();
  21.  
  22. ResultSet resultset = statement.executeQuery(/* MY SQL reqquest */);
  23.  
  24. while(resultset.next()){
  25. System.out.println(resultset.getString(/* THE COLUMN AND ROW I WANTED IN MY REQUEST */));
  26. }
  27.  
  28. }catch(SQLException e){
  29. System.out.println("SQL connection error: " + e.getMessage());
  30. }finally {
  31. if(conn != null){
  32. try{
  33. /* CLosing connection */
  34. conn.close();
  35. }catch (SQLException e){
  36. System.out.println("Error while closing the connection: " + e.getMessage());
  37. }
  38. }
  39. }
  40. }
  41. public static void main(String[] args) {
  42. try{ // Loading the MySQL Connector/J driver
  43. Class.forName("com.mysql.jdbc.Driver");
  44. }catch(ClassNotFoundException e){
  45. System.out.println("Error while loading the Driver: " + e.getMessage());
  46. }
  47. request();
  48. }
  49.  
  50. }
  51.  
  52. import java.sql.Connection;
  53. import java.sql.DriverManager;
  54. import java.sql.ResultSet;
  55. import java.sql.SQLException;
  56. import java.sql.Statement;
  57.  
  58. public void conectarBDMySQL (String user, String password,
  59. String ip, String port, String cat)
  60. {
  61. if (conexionMySQL == null)
  62. {
  63. String urlConexionMySQL = "";
  64. if (cat!= "")
  65. urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port+ "/" + cat;
  66. else
  67. urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port;
  68. if (user!= "" & password!= "" & ip != "" & port!= "")
  69. {
  70. try
  71. {
  72. Class.forName("com.mysql.jdbc.Driver");
  73. conexionMySQL = DriverManager.getConnection(urlConexionMySQL,
  74. user, password);
  75. }
  76. catch (ClassNotFoundException e)
  77. {
  78. Toast.makeText(getApplicationContext(),
  79. "Error: " + e.getMessage(),
  80. Toast.LENGTH_SHORT).show();
  81. }
  82. catch (SQLException e)
  83. {
  84. Toast.makeText(getApplicationContext(),
  85. "Error: " + e.getMessage(),
  86. Toast.LENGTH_SHORT).show();
  87. }
  88. }
  89. }
  90. }
Add Comment
Please, Sign In to add comment