Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.*;
- public class Mysql {
- Connection connection;
- public Mysql() throws SQLException {
- connection = DriverManager.getConnection("jdbc:Mysql://35.230.122.14:3306","root","1234567890");
- }
- public String getData() throws SQLException {
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("select * from ooad.Goal");
- String buf = "";
- while(rs.next()) {
- buf = rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4);
- System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
- }
- return buf;
- }
- public void insertData(int index,String name,String start , String end) throws SQLException {
- try {
- String insertQueryStatement = "INSERT INTO ooad.Goal VALUES (?,?,?,?)";
- PreparedStatement crucifyPrepareStat = connection.prepareStatement(insertQueryStatement);
- crucifyPrepareStat.setInt(1, index);
- crucifyPrepareStat.setString(2, name);
- crucifyPrepareStat.setString(3, start);
- crucifyPrepareStat.setString(4, end);
- // execute insert SQL statement
- crucifyPrepareStat.executeUpdate();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public void delete(int index) throws SQLException {
- try {
- String deleteQueryStatement = "delete from ooad.Goal where idGoal = ?";
- PreparedStatement crucifyPrepareStat = connection.prepareStatement(deleteQueryStatement);
- crucifyPrepareStat.setInt(1, index);
- // execute insert SQL statement
- crucifyPrepareStat.executeUpdate();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- public void update(int index,String name,String start , String end) throws SQLException {
- try {
- String deleteQueryStatement = "UPDATE ooad.Goal SET end=?, start=?, nameGoal=? WHERE idGoal=?";
- PreparedStatement crucifyPrepareStat = connection.prepareStatement(deleteQueryStatement);
- crucifyPrepareStat.setInt(4, index);
- crucifyPrepareStat.setString(3, name);
- crucifyPrepareStat.setString(2, start);
- crucifyPrepareStat.setString(1, end);
- // execute insert SQL statement
- crucifyPrepareStat.executeUpdate();
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment