BigETI

MySQLTest

Aug 31st, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4.  
  5. import com.mysql.jdbc.Connection;
  6. import com.mysql.jdbc.Statement;
  7.  
  8.  
  9. public class MySQLTest {
  10. private Connection connect = null;
  11. private Statement statement = null;
  12. private static String MYSQL_HOST = "hostname";
  13. private static String MYSQL_DB = "db";
  14. private static String MYSQL_TABLE = "table";
  15. private static String MYSQL_USER = "user";
  16. private static String MYSQL_PASSWORD = "password";
  17.  
  18.  
  19. public MySQLTest() throws ClassNotFoundException, SQLException
  20. {
  21. Class.forName("com.mysql.jdbc.Driver");
  22. connect = (Connection) DriverManager.getConnection("jdbc:mysql://" + MYSQL_HOST + "/" + MYSQL_DB + "?user=" + MYSQL_USER + "&password=" + MYSQL_PASSWORD);
  23. }
  24.  
  25. public void getResults() throws SQLException
  26. {
  27. statement = (Statement) connect.createStatement();
  28. printMyResults(statement.executeQuery("SELECT * from `" + MYSQL_DB + "`.`" + MYSQL_TABLE + "`"));
  29. connect.close();
  30. }
  31.  
  32. private void printMyResults(ResultSet result_set) throws SQLException
  33. {
  34. long i = 1;
  35. if(result_set != null)
  36. {
  37. while (result_set.next())
  38. {
  39. System.out.println(i++ + ".: " + result_set.getString("descr") + "\r\n\r\n");
  40. }
  41. result_set.close();
  42. }
  43. }
  44. }
Add Comment
Please, Sign In to add comment