您的SQL语法中有错误(com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException :)

时间:2016-09-24 05:05:34

标签: java mysql

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.InputStream;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class Demo extends JFrame {

    public static final String DATABASE_NAME = "nayaz";
    public static final String DATABASE_SERVER = "localhost";
    public static final String DATABASE_USER_ID = "root";
    public static final String DATABASE_PASSWORD = "";
    public static final String connection_url = "jdbc:mysql://" + DATABASE_SERVER + "/" + DATABASE_NAME;

    Connection connection = null;

    File selectedFile;
    Statement ps = null;
    ResultSet rs = null;
    String filePath = null;
    private JTextField textField;

    public Demo() {
        // TODO Auto-generated constructor stub
        super("Patient Details Form");
        setSize(858, 531);
        getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("Pick a file");
        lblNewLabel.setBounds(10, 115, 151, 32);
        getContentPane().add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(134, 115, 352, 32);
        getContentPane().add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("Browse");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                try {

                    JFileChooser fileChooser = new JFileChooser(new File("C:\\"));
                    fileChooser.setMultiSelectionEnabled(false);
                    fileChooser.setVisible(true);

                    int returnValue = fileChooser.showOpenDialog(null);
                    if (returnValue == JFileChooser.APPROVE_OPTION) {
                        selectedFile = fileChooser.getSelectedFile();
                        // selectedFile.getAbsolutePath();
                        if (selectedFile != null) {
                            filePath = selectedFile.getPath();
                            System.out.println(filePath);
                        }
                        if (filePath != null) {
                            textField.setText("File:" + " " + filePath);
                            InputStream input = getClass().getResourceAsStream(filePath);
                            System.out.println(input);
                        }

                        System.out.println(selectedFile.getName());
                    }

                } catch (Exception e1) {

                    JOptionPane.showInputDialog(this, e1.getMessage());
                    e1.printStackTrace();
                }

            }

        });
        btnNewButton.setBounds(195, 188, 169, 23);
        getContentPane().add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Load");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                if (filePath != null) {
                    try {
                        Connection connection = DriverManager.getConnection(connection_url, DATABASE_USER_ID,
                                DATABASE_PASSWORD);
                        ps = connection.createStatement();

                        String query = "LOAD DATA LOCAL INFILE " + filePath + " patient1 " + "FIELDS TERMINATED BY ',' "
                                + "ENCLOSED BY '//' " + "LINES TERMINATED BY '\r\n' ";

                        int val = 0;

                        val = ps.executeUpdate(query);

                        if (val >= 1)
                            JOptionPane.showInputDialog(this, "Succesfully Stored...");
                        else
                            JOptionPane.showInputDialog(this, "Error in storage...");

                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
        });
        btnNewButton_1.setBounds(396, 188, 137, 23);
        getContentPane().add(btnNewButton_1);
    }

    public static void main(String args[]) {
                    Demo sfc = new Demo();
                    sfc.setVisible(true);
    }
  

问题

我正在尝试将csv文件数据加载到mysql表但我无法做到这一点我得到一些异常(com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException)。  以下是我收到的错误请帮我解决此问题。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'C:\Users\Arjun\Desktop\Nayaz\TerraBlueXT\OpenSignalsFiles1-7.csv patient1 FIELDS' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
    at com.nayaz.patient.Demo$2.actionPerformed(Demo.java:134)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
  

图片

enter image description here

0 个答案:

没有答案