· 8 years ago · Feb 28, 2018, 04:58 PM
1package Main;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.SQLException;
7
8public class Tester {
9 // field that we need
10 private static Connection db;
11 private static PreparedStatement stmt;
12 // connection
13 final static String URL = "jdbc:mysql://localhost:3106/";
14 final static String DB_NAME = "evgany";
15 final static String USER_NAME = "root";
16 final static String USER_PASS = "";
17
18 public static void main(String[] args) throws Exception {
19 //creating our connectiuon string
20 db = DriverManager.getConnection(URL+DB_NAME, USER_NAME, USER_PASS);
21
22 //create a table for the first time, and if the table exists do not create it...
23 createTable();
24 }
25
26 public static void createTable()
27 {
28 String sql="CREATE TABLE IF NOT EXISTS WEED (id INT PRIMARY KEY AUTO_INCReMeNT, name VARCHAR(16) NOT NULL, qty int(2)";
29 try{
30 //prepare statment for given SQL
31 stmt = db.prepareStatement(sql);
32 //execure the fucking statment
33 stmt.execute();
34 }
35 catch (SQLException e){
36 e.printStackTrace();
37 }
38 System.out.println("Table was created.......");
39 }
40
41}