Guest User

Untitled

a guest
Oct 5th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. package configcl.util;
  2. import java.sql.*;
  3.  
  4. public class MySqlConnMan {
  5. public static Connection establishConnection(String host, String port, String usr, String pwd) {
  6. Connection con = null;
  7. try {
  8. Class.forName("com.mysql.cj.jdbc.Driver");
  9. con = DriverManager.getConnection(
  10. "jdbc:mysql://" + host + ":" + port + "/generatedata", usr, pwd);
  11. } catch (SQLException ex) {
  12. // handle any errors
  13. System.out.println("SQLException: " + ex.getMessage());
  14. System.out.println("SQLState: " + ex.getSQLState());
  15. System.out.println("VendorError: " + ex.getErrorCode());
  16. } catch (ClassNotFoundException cne) {
  17. System.out.println("ClassNotFoundException: " + cne.getMessage());
  18. }
  19. return con;
  20. }
  21.  
  22. public static String selectData(Connection con) {
  23. String returnable = "";
  24. try {
  25. Statement stmt = con.createStatement();
  26. ResultSet rs = stmt.executeQuery("select * from gd_cities");
  27. while (rs.next()){
  28. // System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
  29. returnable += rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + "\n";
  30. }
  31. con.close();
  32. } catch (Exception e) {
  33. System.out.println(e);
  34. }
  35. return returnable;
  36. }
  37. }
Add Comment
Please, Sign In to add comment