Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MyDB;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class Tester {
- //to create a connection to our Data Base
- private static Connection db;
- //will convert our SQL request to a statement that the DB will recognize
- private static PreparedStatement statement;
- //address of our mysql
- //final static String URL = "jdbc:mysql://127.0.0.1:3306/";
- final static String URL = "jdbc:mysql://localhost/";
- final static String OLDJDBC = "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- //name of the data base
- final static String DB_NAME = "wtf";
- //name of user (in our case administrator)
- final static String DB_USER = "root";
- //administrator password
- final static String DB_PASS = "";
- public static void main(String[] args) throws SQLException {
- //create our connection string
- //creating a connection to our data base, using our new library
- db = DriverManager.getConnection(URL+DB_NAME+OLDJDBC,DB_USER,DB_PASS);
- //create table for the first time if it's not exists
- createTable();
- }
- private static void createTable() {
- //create SQL statment
- String sql = "CREATE TABLE IF NOT EXISTS drugs "+
- "(id INT PRIMARY KEY AUTO_INCREMENT, "+
- "name VARCHAR (16) NOT NULL);";
- try{
- //prepare a SQL command
- statement = db.prepareStatement(sql);
- //execute the SQL command
- statement.execute();
- System.out.println("Table was created");
- } catch (SQLException err){
- System.out.println(err.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement