Guest User

Untitled

a guest
Jan 6th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.sql.ResultSet;
  6.  
  7.  
  8. public class Handler {
  9. private String address="";
  10. private String username="";
  11. private String password="";
  12. private String databaseName="";
  13.  
  14. public Handler(String address, String username, String password, String databaaseName) {
  15. this.address = address;
  16. this.username = username;
  17. this.password = password;
  18. this.databaseName = databaaseName;
  19. }
  20.  
  21. public Handler(String address, String username, String password) {
  22. this.address = address;
  23. this.username = username;
  24. this.password = password;
  25. }
  26.  
  27. public String getAddress(){
  28. return address;
  29. }
  30.  
  31. public void setAddress(String address) {
  32. this.address = address;
  33. }
  34.  
  35. public String getUsername() {
  36. return username;
  37. }
  38.  
  39. public String getPassword() {
  40. return password;
  41. }
  42.  
  43. public String getDatabaseName() {
  44. return databaseName;
  45. }
  46.  
  47. /*
  48. public boolean connect() throws SQLException{
  49. Connection conn=null;
  50. String uri="";
  51. uri="jdbc:mysql://"+this.getAddress()+"/"+this.getDatabaseName()+"?user="+this.getUsername()+"&password="+this.getPassword();
  52. try {
  53. //Do something with the connection
  54. conn = DriverManager.getConnection(uri);
  55. return true;
  56. }catch(SQLException ex ){
  57. //Catch errors
  58. System.out.println("SQLException: " + ex.getMessage());
  59. System.out.println("SQLState: " + ex.getSQLState());
  60. return false;
  61. }
  62. }
  63. */
  64.  
  65. public static void main(String[] args){
  66. Connection conn = null;
  67. String uri = "";
  68. uri = "jdbc:mysql://"+this.getAddress()+"/"+this.getDatabaseName()+"?user="+this.getUsername()+"&password="+this.getPassword();
  69. try {
  70. conn = DriverManager.getConnection(uri);
  71. }catch(SQLException ex ){
  72. System.out.println("SQLException: " + ex.getMessage());
  73. System.out.println("SQLState: " + ex.getSQLState());
  74. }
  75.  
  76. Statement stmt = null;
  77. ResultSet rs = null;
  78.  
  79. try{
  80. stmt=conn.createStatement();
  81. if (stmt.execute("SELECT FROM ")) {
  82. rs = stmt.getResultSet();
  83. }
  84. }catch (SQLException ex){
  85. System.out.println("SQLException: " + ex.getMessage());
  86. System.out.println("SQLState: " + ex.getSQLState());
  87.  
  88. }
  89.  
  90. }
  91.  
  92. }
Add Comment
Please, Sign In to add comment