Guest User

Untitled

a guest
Nov 18th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. package sample;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.*;
  8.  
  9.  
  10. public class Mysql {
  11.  
  12. Connection connection;
  13.  
  14. public Mysql() throws SQLException {
  15. connection = DriverManager.getConnection("jdbc:Mysql://35.230.122.14:3306","root","1234567890");
  16. }
  17.  
  18. public String getData() throws SQLException {
  19. Statement stmt = connection.createStatement();
  20. ResultSet rs = stmt.executeQuery("select * from ooad.Goal");
  21. String buf = "";
  22. while(rs.next()) {
  23. buf = rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4);
  24. System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
  25. }
  26. return buf;
  27. }
  28.  
  29. public void insertData(int index,String name,String start , String end) throws SQLException {
  30.  
  31. try {
  32. String insertQueryStatement = "INSERT INTO ooad.Goal VALUES (?,?,?,?)";
  33.  
  34. PreparedStatement crucifyPrepareStat = connection.prepareStatement(insertQueryStatement);
  35. crucifyPrepareStat.setInt(1, index);
  36. crucifyPrepareStat.setString(2, name);
  37. crucifyPrepareStat.setString(3, start);
  38. crucifyPrepareStat.setString(4, end);
  39. // execute insert SQL statement
  40. crucifyPrepareStat.executeUpdate();
  41. }catch (SQLException e){
  42. e.printStackTrace();
  43. }
  44. }
  45. public void delete(int index) throws SQLException {
  46.  
  47. try {
  48. String deleteQueryStatement = "delete from ooad.Goal where idGoal = ?";
  49.  
  50. PreparedStatement crucifyPrepareStat = connection.prepareStatement(deleteQueryStatement);
  51. crucifyPrepareStat.setInt(1, index);
  52.  
  53. // execute insert SQL statement
  54. crucifyPrepareStat.executeUpdate();
  55. }catch (SQLException e){
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. public void update(int index,String name,String start , String end) throws SQLException {
  61.  
  62. try {
  63. String deleteQueryStatement = "UPDATE ooad.Goal SET end=?, start=?, nameGoal=? WHERE idGoal=?";
  64.  
  65. PreparedStatement crucifyPrepareStat = connection.prepareStatement(deleteQueryStatement);
  66. crucifyPrepareStat.setInt(4, index);
  67. crucifyPrepareStat.setString(3, name);
  68. crucifyPrepareStat.setString(2, start);
  69. crucifyPrepareStat.setString(1, end);
  70. // execute insert SQL statement
  71. crucifyPrepareStat.executeUpdate();
  72. }catch (SQLException e){
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. }
Add Comment
Please, Sign In to add comment