0

I'm trying to make the program read the "Entity.sql" File and then execute it to make it run in phpmyadmin. I did used a mysql connector to link netbeans to mysql and then it should used the Connection for phpmyadmin to execute the Statement (Entity.sql)

The Entity.sql file contains as sql Statement:

CREATE DATABASE IF NOT EXISTS Student 

The problem is that it's not working and it's giving me this error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

here is my entire code:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
import java.io.*;
import java.util.*;


public class SQLConnecter {
    private Connection con;
    private Statement st;
    private ResultSet rs;

        public SQLConnecter(){

        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","");
            st = con.createStatement();                                  
        }

        catch(Exception ex){
            System.out.println("Error:"+ ex);
       }
    }


public void getData(){

    try{
         Scanner infile = new Scanner(new File("Entity.sql"));
    String sql = infile.next();
    while(infile.hasNext()){
    st.executeUpdate(sql);
       }

    infile.close();
}
    catch(Exception ex){
        System.out.println(ex);
    }  
 }
public static void main(String args[]){
SQLConnecter connect = new SQLConnecter();
connect.getData();
}
}

I want the program to first

1- Read the Entity.sql file

2- Execute the file into phpmyadmin

Can someone please help me to solve this?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Lana
  • 171
  • 1
  • 7
  • 18
  • Don't use Javascript snippets for other code. – Boris the Spider Feb 08 '15 at 11:41
  • 1
    Why do you want to use phpmyadmin? Why not call the database code directly? – Boris the Spider Feb 08 '15 at 11:42
  • i dont want to retrieve from database. I want to read the file and send it to phpmyadmin. The file contain sql statement. Do you have any idea? @BoristheSpider – Lana Feb 08 '15 at 11:53
  • 1
    I don't understand where phpmyadmin comes into it. Anything phpmyadmin can do, Java can do via JDBC. I don't understand why you need to bring in phpmyadmin - it adds complexity and nothing else. – Boris the Spider Feb 08 '15 at 11:55
  • In fact i have a project on ERD CONVERTER, where the user must be able to convert its drawing to tables. But first he must choose within a list of databases where phpmyadmin is included. So for now am trying to make the script, once the program read the script (.sql file) , the file is executed in phpmyadmin(suppose the user chose that). I did implemented part of it but it's giving me error. Can you advice? @BoristheSpider – Lana Feb 08 '15 at 12:00
  • Try using ScriptRunner with MyBatis to execute sql file. http://stackoverflow.com/a/19329826 – hrakup Feb 08 '15 at 12:23
  • Thank you. Do i need to import anything for org.apache.ibatis.jdbc.ScriptRunner; @hrakup – Lana Feb 08 '15 at 12:34
  • Can i use this too for other database like Postgresql? @hrakup – Lana Feb 08 '15 at 14:34
  • 1
    MySQL is a database. phpMyAdmin is a tool administrators use to interact with and manage a MySQL database (or MariaDB or Drizzle). You can't use phpMyAdmin to interact with PostgreSQL, but then again nothing you're doing here in the code shown has anything to do with phpMyAdmin anyway. – Isaac Bennetch Feb 09 '15 at 19:39

0 Answers0