JAVA:ExecuteUpdate上的错误

时间:2015-10-04 20:32:01

标签: java

请我想添加一个元素到数据库但我仍然有这个错误我不知道确切的错误在哪里,我确定sql查询和我的数据库

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 'Ahmed,Saleh,2001/12/25,Cité les jasmins N°55, Sfax 3020,Saleh.BenAhmed@gmail.c' 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:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at utils.DBHelper.executeUpdate(DBHelper.java:78)
    at dao.EtudiantDAO.Ajout(EtudiantDAO.java:21)
    at dao.EtudiantDAO.main(EtudiantDAO.java:30)
Exception in thread "main" java.lang.NullPointerException
    at utils.DBHelper.closePreparedStatement(DBHelper.java:106)
    at utils.DBHelper.close(DBHelper.java:87)
    at dao.EtudiantDAO.Ajout(EtudiantDAO.java:22)
    at dao.EtudiantDAO.main(EtudiantDAO.java:30)

这些是我的课程: DBHelper.java

package utils;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;


public class DBHelper {
    public static Connection conn=null;
    static Statement stmt;
    private static PreparedStatement pstmt;
    private ResultSet rs;
    private static String driver_class_name="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://localhost/gestetudiant";
    private static String username="root";
    private static String password="";

    public DBHelper(){


    }

    public static Connection getConnection(){



        try{
            Class.forName(driver_class_name);
            conn= DriverManager.getConnection(url, username, password);
        }
        catch(SQLException e){
            e.printStackTrace();
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        return (Connection) conn;

    }


    public static PreparedStatement getPreparedStatement(String sql){
        try {
            pstmt= getConnection().prepareStatement(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return pstmt;
    }

    public static java.sql.Statement getStatement(){
        try {
            /* Création de l'objet gérant les requêtes */
            stmt = conn.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stmt;
    }

    public ResultSet executeQuery(String sql){
        try {
            rs=getStatement().executeQuery(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }

    public static void executeUpdate(String sql){
        try {
            getStatement().executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

    public static void close(){
        closeConnection();
        closePreparedStatement();
        closeStatement();

    }

    private static void closeStatement() {
        // TODO Auto-generated method stub
        try {
            stmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void closePreparedStatement() {
        // TODO Auto-generated method stub
        try {
            pstmt.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static void closeConnection() {
        // TODO Auto-generated method stub
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Etudiant.java

package model;

import java.util.Date;

/**
 * @author jk
 *
 */
public class Etudiant {

    private String cin;
    private String nom;
    private String prenom;
    private String datenaissance;
    private String adresse;
    private String mail;
    private String tel;

    public Etudiant(String cin, String nom, String prenom, String string, String adresse, String mail,
            String tel) {
        super();
        this.cin = cin;
        this.nom = nom;
        this.prenom = prenom;
        this.datenaissance = string;
        this.adresse = adresse;
        this.mail = mail;
        this.tel = tel;
    }

    public Etudiant() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void finalize() throws Throwable   
    {
        super.finalize();  
    }

    public String getCin() {
        return cin;
    }

    public void setCin(String cin) {
        this.cin = cin;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getDatenaissance() {
        return datenaissance;
    }

    public void setDatenaissance(String datenaissance) {
        this.datenaissance = datenaissance;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }




}

EtudiantDAO.java

package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;


import model.Etudiant;
import utils.DBHelper;

public class EtudiantDAO {

    private static Connection conn;

    private static void Ajout(Etudiant pObjEtud)

    {
        conn=DBHelper.getConnection();
        DBHelper.executeUpdate("INSERT into etudiant(cin,nom,prenom,datenaissance,adresse,mail,tel) VALUES("+pObjEtud.getCin()+","+pObjEtud.getNom()+","+pObjEtud.getPrenom()+","+pObjEtud.getDatenaissance()+","+pObjEtud.getAdresse()+","+pObjEtud.getMail()+","+pObjEtud.getTel()+")");
        DBHelper.close();


    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Etudiant e=new Etudiant("0256341","Ben Ahmed","Saleh","2001/12/25","Cité les jasmins N°55, Sfax 3020","Saleh.BenAhmed@gmail.com","+216 55223344");
        Ajout(e);


    }



}

3 个答案:

答案 0 :(得分:1)

基于文本的字段需要用引号括起来。使用getPreparedStatement

DBHelper方法
PreparedStatement ps = 
    DBHelper.getPreparedStatement("INSERT into etudiant(cin,nom,prenom,datenaissance,adresse,mail,tel) VALUES(?,?,?,?,?,?,?)");
ps.setString(1, pObjEtud.getCin());
...
ps.executeUpdate();

答案 1 :(得分:0)

我认为你在SQL查询中缺少["]。我建议使用PreparedStatement会有所帮助(无论如何都是最佳实践):

    String sql = "INSERT into etudiant"
            + "(cin,nom,prenom,datenaissance,adresse,mail,tel) VALUES"
            + "(?,?,?,?,?,?,?)";
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setString(1, pObjEtud.getCin());
    ps.setString(2, pObjEtud.getNom());
    ps.setString(3, pObjEtud.getPrenom());
    //
    // continue all fields ...
    //
    ps.executeUpdate();

答案 2 :(得分:0)

Kilani,如果你问为什么会抛出异常,答案是基本的。

在您的main方法中,您调用基本上调用Ajout的{​​{1}}方法,并在executeUpdate操作后调用close操作。

但在这里你只有“更新声明”。您没有executeUpdate关闭。 PrepareStatement变量从未初始化,但您尝试关闭它。

也许您可以检查pstmt是否为null。就是这样。

pstmt

...

public static void executeUpdate(String sql){
    try {
        getStatement().executeUpdate(sql);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

public static void close(){
    closeConnection();
    closePreparedStatement();
    closeStatement();

}

这些是您解决问题的基本功能。

1)在Ajout函数中,调用executeUpdate。只有psmt

2)之后你调用close函数。当您调用executeUpdate时,您已启动stmt变量。不是pstmt。但是当你调用close()函数时,你会尝试关闭stmt和pstmt。由于stmt不是NULL,因此没有问题。但是pstmt是null ..这里它抛出NullPointer异常。

相关问题