Guest User

Untitled

a guest
Jan 6th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 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 databaseName) {
  15. this.address = address;
  16. this.username = username;
  17. this.password = password;
  18. this.databaseName = databaseName;
  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 String getUsername() {
  32. return username;
  33. }
  34.  
  35. public String getPassword() {
  36. return password;
  37. }
  38.  
  39. public String getDatabaseName() {
  40. return databaseName;
  41. }
  42.  
  43. public static Connection conn = null;
  44.  
  45. public void connect(){
  46. String uri = "";
  47. uri = "jdbc:mysql://"+this.getAddress()+"/"+this.getDatabaseName()+"?user="+this.getUsername()+"&password="+this.getPassword();
  48. try {
  49. conn = DriverManager.getConnection(uri);
  50. }catch(SQLException ex ){
  51. System.out.println("SQLException: " + ex.getMessage());
  52. System.out.println("SQLState: " + ex.getSQLState());
  53. }
  54. }
  55.  
  56. public static void main(String[] args){
  57.  
  58. Statement stmt = null;
  59. ResultSet rs = null;
  60.  
  61. //Get a record
  62. try{
  63. stmt=conn.createStatement();
  64. if (stmt.execute("SELECT * FROM Paziente ")) {
  65. rs = stmt.getResultSet();
  66. conn.close();
  67. }
  68. }catch (SQLException ex){
  69. System.out.println("SQLException: " + ex.getMessage());
  70. System.out.println("SQLState: " + ex.getSQLState());
  71. }
  72.  
  73. //Insert a record
  74. try{
  75. stmt=conn.createStatement();
  76. stmt.executeUpdate("INSERT INTO Reparto " + "VALUES (Chirurgia)");
  77. conn.close();
  78.  
  79. }catch (SQLException ex){
  80. System.out.println("SQLException: " + ex.getMessage());
  81. System.out.println("SQLState: " + ex.getSQLState());
  82. }
  83.  
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment