Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ConnectionToDataBase {
- private Connection connection = null;
- private Statement statement;
- public Connection getConnection() {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","");
- System.out.println("Success!");
- }
- catch(Exception e) {
- System.out.println(e);
- }
- return connection;
- }
- private void createDatabase(String databaseName) {
- try {
- this.connection = getConnection();
- Statement s = connection.createStatement();
- s.executeUpdate("CREATE DATABASE IF NOT EXISTS " + databaseName);
- this.connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+databaseName,"root","");
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void createTable(String tableName) {
- String myTableName = "CREATE TABLE " + tableName + " ( "
- + "id INT(64) NOT NULL AUTO_INCREMENT,"
- + "name VARCHAR(20),"
- + "date DATE,"
- + "agentCount INT(64), "
- + "PRIMARY KEY (`id`))";
- try {
- statement = connection.createStatement();
- //This line has the issue
- statement.executeUpdate(myTableName);
- System.out.println("Table Created");
- } catch (SQLException e ) {
- System.out.println("An error has occured on Table Creation");
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- ConnectionToDataBase c = new ConnectionToDataBase();
- c.createDatabase("sashoDB");
- c.createTable("myNewTable");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement