Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SQL;
- import java.sql.*;
- public class QueryExample {
- private static Connection conn = null;
- private static Connection getInstance() {
- if (conn==null) new QueryExample();
- return conn;
- }
- private QueryExample() {
- String url = "jdbc:mysql://";
- String user = "root";
- String password = "";
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection conn = DriverManager.getConnection(url, user, password);
- } catch (Exception e) {
- e.printStackTrace();
- }/*
- } catch (ClassNotFoundException e) {
- System.out.println("Treiber nicht gefunden");
- } catch (SQLException e) {
- System.out.println("Connect nicht moeglich");
- }*/
- }
- public static void createTable(){
- try{
- conn = getInstance();
- PreparedStatement create = conn.prepareStatement(
- "CREATE TABLE IF NOT EXISTS appointments(number INT, difficulty INT");
- create.executeUpdate();
- } catch(SQLException e) {
- e.printStackTrace();
- }
- }
- public static void createDatabase(){
- try {
- Statement statem = conn.createStatement();
- statem.execute("CREATE DATABASE IF NOT EXISTS pmt");
- statem.execute("USE pmt");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void connect() {
- String url = "jdbc:mysql://";
- String user = "root";
- String password = "";
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection conn = DriverManager.getConnection(url, user, password);
- // statem.execute("INSERT INTO pmt (number, difficulty) VALUES"
- // + "('1', '10'), ('2', '20'), ('3', '30'), ('4', '45'), ('5', '50')"
- // + "('6', '55'), ('7', '70'), ('8', '75'), ('9', '85'), ('10', '95')");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- QueryExample.getInstance();
- QueryExample.connect();
- QueryExample.createDatabase();
- QueryExample.createTable();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement