Je travaille actuellement sur un projet pour l'école qui est une application mémo java. Il fonctionne sur l'approche du contrôleur de vue de modèle d'une application, de sorte que de nombreux fichiers gèrent chacun certaines tâches. Quand je cours ma classe de createMemos j'obtiens une erreur de syntaxe de sql. qui se lit comme suit .... Voyez-vous des erreurs de syntaxe?Erreur de syntaxe MySQL Application Java Memo
run:
Model used mysql
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
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
at models.Memos.createTable(Memos.java:49)
at guimemos.CreateMemos.main(CreateMemos.java:15)
BUILD SUCCESSFUL (total time: 1 second)
ma classe de mémos est la suivante. .
public class Memos {
private String table = "memos";
private String props_file = "models" + java.io.File.separator + table + ".properties";
private DB db;
public Memos() throws Exception {
db = new DB();
}
public void createTable() throws Exception {
Properties table_props = Util.loadFile(props_file);
String property = DB.getModel() + "." + table;
String table_def = table_props.getProperty(property);
if (table_def == null) {
throw new Exception("no such property: " + property);
}
Connection cx = db.connect();
String sql_op;
Statement st = cx.createStatement();
sql_op = "drop table if exists " + table;
st.executeUpdate(sql_op);
sql_op = "create table " + table + "(" + table_def + ")";
st.executeUpdate(sql_op);
}
public int insert(Memo memo) throws Exception {
Connection cx = db.connect();
PreparedStatement st = cx.prepareStatement(
"insert into " + table + "(title,timeStamp,content) values (?, ?, ?)");
st.setString(1, memo.getTitle());
st.setTimestamp(2, memo.getTimeStamp());
// st.setString(2, memo.getTimeStamp().toString());
st.setString(3, memo.getContent());
st.executeUpdate();
st = cx.prepareStatement("select max(id) from " + table);
ResultSet rs = st.executeQuery();
rs.next();
int new_id = rs.getInt(1);
return new_id;
}
// fetch all memos
public List<Memo> fetchAll() throws Exception {
Connection cx = db.connect();
String sql_op = "select * from " + table;
Statement st = cx.createStatement();
ResultSet rs = st.executeQuery(sql_op);
List<Memo> L = new LinkedList<Memo>();
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String content = rs.getString("content");
Timestamp timeStamp = rs.getTimestamp("timeStamp");
Memo memo = new Memo(id, title, timeStamp, content);
L.add(memo);
}
return L;
}
// fetch one memo
public Memo fetch(int id) throws Exception {
Connection cx = db.connect();
PreparedStatement st = cx.prepareStatement(
"select * from " + table + " where id=?");
st.setInt(1, id);
ResultSet rs = st.executeQuery();
if (!rs.next()) {
return null;
}
String title = rs.getString("title");
String content = rs.getString("content");
Timestamp timeStamp = rs.getTimestamp("timeStamp");
Memo memo = new Memo(id, title, timeStamp, content);
return memo;
}
// remove one memo
public boolean remove(int id) throws Exception {
Connection cx = db.connect();
PreparedStatement st = cx.prepareStatement(
"delete from " + table + " where id=?");
st.setInt(1, id);
int num = st.executeUpdate();
return (num != 0);
}
// update one memo
public void modify(Memo memo) throws Exception {
Connection cx = db.connect();
int id = memo.getId();
PreparedStatement st = cx.prepareStatement("update " + table
+ " set title=?, timeStamp=?, content=?" + " where id=?");
st.setString(1, memo.getTitle());
st.setTimestamp(2, memo.getTimeStamp());
st.setString(3, memo.getContent());
st.setInt(4, id);
st.executeUpdate();
}
}
est le fichier de propriétés ici:
mysql.memos=id integer auto_increment primary key not null,\
title varchar(80) not null,
timeStamp datetime not null,\
content text
sqlite.memos=id integer primary key not null,\
title text not null,\
timeStamp int not null,\
content text
erreur
après avoir ajouté des suggestions
Model used mysql
Attempting to execute SQL: drop table if exists memos
Attempting to execute SQL: create table memos(id integer auto_increment primary key not null,title varchar(80) not null,)
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
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39 )
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
at models.Memos.createTable(Memos.java:50)
at guimemos.CreateMemos.main(CreateMemos.java:15)
Essayez d'imprimer ce que SQL est que vous tiriez –
Quelle est votre table_def? – Kayser
Ouais moi aussi, pouvez-vous essayer d'enregistrer la requête sql complète dans un var et de les fournir pour nous? –