简体   繁体   English

MySQL语法错误Java Memo应用程序

[英]MySQL syntax error Java Memo application

I am currently working on a project for school that is a java memo application. 我目前正在为Java备忘录应用程序开发一个学校项目。 It runs on the model view controller approach to an application so there are many files that each handle certain tasks. 它以应用程序的模型视图控制器方法运行,因此有许多文件可以处理某些任务。 When I run my createMemos class i get an sql syntax error. 当我运行createMemos类时,出现sql语法错误。 that reads as follows.... Do you see any syntax mistakes? 内容如下:...您是否看到语法错误?

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)

my memos class is as follows . 我的备忘课如下。 .

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();
    }
}

here is the properties file: 这是属性文件:

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

error after adding 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)

It looks like something is wrong with table_def in this statement: 该语句中的table_def似乎有问题:

sql_op = "create table " + table + "(" + table_def + ")";
st.executeUpdate(sql_op);

You're reading it from a properties file though, so we'll need to actually see it to know what's wrong. 不过,您是从属性文件中读取它的,因此我们需要实际查看它才能知道出了什么问题。 I'd try changing it to this: 我尝试将其更改为:

sql_op = "create table " + table + "(" + table_def + ")";
System.out.println("Attempting to execute SQL: " + sql_op);
st.executeUpdate(sql_op);

And then let us know what the output is. 然后让我们知道输出是什么。

EDIT: The problem is this section: 编辑:问题是此部分:

mysql.memos=id integer auto_increment primary key not null,\
title varchar(80) not null,
timeStamp datetime not null,\
content text

Note the lack of a \\ on the second line. 请注意,第二行上没有\\ That's causing your property to be cut off, so you lose the timeStamp and content lines, and it's a syntax error since there's a trailing , . 这是造成被切断你的财产,让你失去了时间戳和内容行,这是一个语法错误,因为有一个尾随,

Try changing to this: 尝试更改为此:

mysql.memos=id integer auto_increment primary key not null,\
title varchar(80) not null,\
timeStamp datetime not null,\
content text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM