- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请我想向数据库添加一个元素,但我仍然遇到此错误,我不知道错误到底在哪里,并且我确定 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)
这些是我的类(class):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();
}
}
}
学生.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);
}
}
最佳答案
基于文本的字段需要用引号引起来。使用DBHelper
的getPreparedStatement
方法
PreparedStatement ps =
DBHelper.getPreparedStatement("INSERT into etudiant(cin,nom,prenom,datenaissance,adresse,mail,tel) VALUES(?,?,?,?,?,?,?)");
ps.setString(1, pObjEtud.getCin());
...
ps.executeUpdate();
关于JAVA : error on ExecuteUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32938074/
这个问题已经困扰两天了。尝试了我能想到的一切。非常感谢您在这个问题上给我的任何帮助。提前致谢。` try { // try get tx records Class.forName("com
我有如下代码: using (var session = this.sessionCreator.OpenSession()) using (var transaction = session.Beg
我有以下代码: sql = update [myTable] set content = '" map.getString() + "' where id = " map.getKey(); stmt
我创建了这样的声明: String query = "INSERT INTO ZMONES (ID, PAVADINIMAS, SLAPTAZODIS) VALUES(" +sum+" ,"+
请我想向数据库添加一个元素,但我仍然遇到此错误,我不知道错误到底在哪里,并且我确定 sql 查询和我的数据库 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxEr
我想知道,如何关闭 JAVA(JDBC) 中的executeUpdate 语句? 例如: String sql = "UPDATE. .. . .. Whatever..."; ResultSet
PreparedStatement updatestmt1 = con.prepareStatement("update BASE_TX set tx_vl=replace(tx_vl,?,?)");
使用 readline 5.1 运行 mysql Ver 15.1 Distrib 5.5.44-MariaDB,适用于 Linux (x86_64) 我编写了一个 Java servlet,它在 S
今天我遇到了这样的情况:程序启动,正常运行,显示数据已正确输入数据库,但没有执行。我再次运行脚本,它运行良好,但有趣的是它没有抛出异常,尽管它未能插入数据。 是否存在 Statement.execut
你好,祝大家新年快乐。 我正在开发一个 JDBC 程序,它获取服务器中的每个数据库,访问每个表并收集有关这些列的信息。最后,我想将表中的每一行信息添加为行。我的程序可以正确编译和运行,但表总是返回 E
我得到一个异常(exception) com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be t
我正在尝试在某些应用程序中进行手动订购。更新字段的 JPAQL 是这样的: update SortableBean s set s.position = s.position + 1 where s.
这是我的代码: String insertQuery = "INSERT INTO Links(PageId, LinkId) values(?, ?)"; PreparedStatement pre
我一直在尝试执行以下查询: SQL_ADDPROJECT = "INSERT INTO project (name, description, duration, departement, chef)
我的目标是向我创建的现有表中添加一个新行。但是,我收到以下错误。 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have
如果我有一个使用以下架构创建的表: CREATE TABLE names (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR); 还有一个 List na
我正在 JBehave 非 Maven 框架中工作,并且正在运行一个文本文件内的插入语句,我将其作为 qry 传递给我的方法。插入语句为: Insert into croutreach.ACME_OU
我正在修改一个 Java Web 应用程序,该应用程序是以前(由其他人)编写的,它使用 JDBC。该数据库是使用 PHPMyAdmin 创建的。在此过程中,我需要重命名其中一张表。我在代码中用正确的名
所以我试图将信息从java应用程序存储到数据库中,但我收到此错误:“”AWT-EventQueue-0“java.lang.NullPointerException”并且我一生都找不到原因。我尝试更改
我使用以下代码通过 postgres 的更新查询更改密码。 @POST @Path("/changepassword") @Consumes(MediaType.APPLICATION_JSON) p
我是一名优秀的程序员,十分优秀!