Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 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.         //Error:(40, 24) java: <identifier> expected
  40.         try (connection = DriverManager.getConnection(URL,USERNAME,PASSWORD)){
  41.             System.out.println("Connection successful");
  42.         } catch (SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     private void initStatement(){
  48.         //Error:(49, 23) java: <identifier> expected
  49.         try (statement = connection.createStatement()){
  50.             System.out.println("Statement is created");
  51.         } catch (SQLException e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.  
  56.     public void register(int id, String name){
  57.         try {
  58.             statement.executeUpdate("INSERT INTO users (id, name, hp, power) VALUES ("+id+",'"+name+"',100,10)");
  59.         } catch (SQLException e) {
  60.             e.printStackTrace();
  61.         }
  62.     }
  63.  
  64.     public void updateHP(int id, int hp){
  65.         try{
  66.             statement.executeUpdate("UPDATE users SET hp = "+ hp +" where id = "+ id);
  67.         }catch (SQLException e){
  68.             e.printStackTrace();
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement