Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sql;
- import java.sql.*;
- import java.util.ArrayList;
- public class MySql {
- private String host;
- private int port;
- private String username;
- private String password;
- private String database;
- private Connection con;
- private Statement st;
- private ResultSet rs;
- public MySql()
- {
- this.host ="localhost";
- this.port = 3306;
- this.username = "";
- this.password = "";
- this.database = "";
- this.OpenConnection();
- }
- public void OpenConnection()
- {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?user=" + this.username + "&password=" + this.password + "&serverTimezone=UTC");
- st = con.createStatement();
- } catch(Exception ex) {
- System.out.println("ERROR: "+ex);
- }
- }
- public ResultSet query(String query)
- {
- try {
- st = con.prepareStatement(query);
- rs = st.executeQuery(query);
- return rs;
- } catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public ArrayList<String> sqla(String query)
- {
- ResultSet result = query(query);
- ArrayList<String> array = new ArrayList<>();
- if( !result.beforeFirst() )
- {
- return array;
- }
- while(result.next()))
- {
- }
- }
- public int insert(String table, String cond)
- {
- String query = "INSERT INTO " + table + " " + cond;
- try {
- st = con.createStatement();
- return st.executeUpdate(query);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public int update(String table, String cond)
- {
- String query = "UPDATE " + table + "SET " + cond;
- try {
- st = con.createStatement();
- return st.executeUpdate(query);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return 0;
- }
- public int delete(String table, String cond)
- {
- String query = "DELETE FROM " + table + " " + cond;
- try {
- st = con.createStatement();
- return st.executeUpdate(query);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement