Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.testito;
- import java.sql.*;
- /**
- * Created by null on 11.10.2017.
- * Time is: 16:06
- */
- public class MainDB {
- private static final String SSL = "useUnicode=true&useSSL=true&useJDBCCompliantTimezoneShift=true";
- private static final String TIMEZONE = "&useLegacyDatetimeCode=false&serverTimezone=UTC";
- private static final String URL = "jdbc:mysql://localhost:3306/testdatabase?" + SSL + TIMEZONE;
- private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
- private static final String USERNAME = "root";
- private static final String PASSWORD = "root";
- private Connection connection;
- private Statement statement;
- private ResultSet resultSet;
- public MainDB(){
- initDriver();
- initDataBase();
- initStatement();
- }
- public ResultSet getResultSet() {
- return resultSet;
- }
- private void initDriver(){
- try {
- Class.forName(DRIVER);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private void initDataBase(){
- //Error:(40, 24) java: <identifier> expected
- try (connection = DriverManager.getConnection(URL,USERNAME,PASSWORD)){
- System.out.println("Connection successful");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void initStatement(){
- //Error:(49, 23) java: <identifier> expected
- try (statement = connection.createStatement()){
- System.out.println("Statement is created");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void register(int id, String name){
- try {
- statement.executeUpdate("INSERT INTO users (id, name, hp, power) VALUES ("+id+",'"+name+"',100,10)");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void updateHP(int id, int hp){
- try{
- statement.executeUpdate("UPDATE users SET hp = "+ hp +" where id = "+ id);
- }catch (SQLException e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement