Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package com.test.testito;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6.  * Created by null on 11.10.2017.
  7.  * Time is: 16:06
  8.  */
  9. public class MainDB {
  10.     private static final String SSL = "useUnicode=true&useSSL=true&useJDBCCompliantTimezoneShift=true";
  11.     private static final String TIMEZONE = "&useLegacyDatetimeCode=false&serverTimezone=UTC";
  12.     private static final String URL = "jdbc:mysql://localhost:3306/testdatabase?" + SSL + TIMEZONE;
  13.     private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
  14.     private static final String USERNAME = "root";
  15.     private static final String PASSWORD = "root";
  16.     private Connection connection;
  17.     private Statement statement;
  18.     private ResultSet resultSet;
  19.  
  20.     public MainDB(){
  21.         initDriver();
  22.         initDataBase();
  23.         initStatement();
  24.     }
  25.  
  26.     public ResultSet getResultSet() {
  27.         return resultSet;
  28.     }
  29.  
  30.     private void initDriver(){
  31.         try {
  32.             Class.forName(DRIVER);
  33.         } catch (ClassNotFoundException e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.  
  38.     private void initDataBase(){
  39.         try (connection = DriverManager.getConnection(URL,USERNAME,PASSWORD)){
  40.             System.out.println("Connection successful");
  41.         } catch (SQLException e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.  
  46.     private void initStatement(){
  47.         try (statement = connection.createStatement()){
  48.             System.out.println("Statement is created");
  49.         } catch (SQLException e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53.  
  54.     public void register(int id, String name){
  55.         try {
  56.             statement.executeUpdate("INSERT INTO users (id, name, hp, power) VALUES ("+id+",'"+name+"',100,10)");
  57.         } catch (SQLException e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.  
  62.     public void updateHP(int id, int hp){
  63.         try{
  64.             statement.executeUpdate("UPDATE users SET hp = "+ hp +" where id = "+ id);
  65.         }catch (SQLException e){
  66.             e.printStackTrace();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement