为什么不能检索或更新数据库中的数据?

时间:2016-01-19 02:19:30

标签: java mysql japplet

尝试在我的数据库中读取数据并进行更新,但似乎总是会出现异常。为什么我无法阅读和更新任何内容。我知道我与数据库有联系。我相当确定我的SQL语法在我的java代码中是正确的。程序/ applet没有在浏览器中直接在eclipse中运行,所以这应该可以正常工作。

这是我的代码

public class Kreta1 extends JApplet {
private static final long serialVersionUID = -6464646464646464646;//just some nrs

private Vector card;
private int menu_id;
private String name_dish,price,ingredients,recipe;
private Connectie con; 


 class Afhaalmenu1{

        private Connectie con;
        private int menu_id;
        private String name_dish,price,ingredients,recipe;
        private Vector card; 

        public Afhaalmenu1(){
             con= new Connectie();
             card=new Vector();
        }
        public Afhaalmenu1(int menu_id){
            this();
            this.menu_id=menu_id;
        }
            public Afhaalmenu1(int menu_id, String name_dish, String price,String ingredients, String recipe){
                this();
                this.menu_id=menu_id;
                this.name_dish=name_dish;
                this.price=price;
                this.ingredients=ingredients;
                this.recipe=recipe;

            }
            public void setName_dish (String ng){
                this.name_dish=ng;
            }
            public String getName_dish){
                return name_dish;
            }
            public void setPrice (String pr){
                this.price=pr;
            }
            public String getPrice(){
                return price;
            }
            public void setIngredients(String ingr){
                this.ingredients=ingr;
            }
            public String getIngredients(){
                return ingredients;
            }
            public void setRecipe(String rec){
                this.recipe=rec;
            }
            public String getRecipe(){
                return recipe;
            }
            public void setMenu_id(int I){
                this.menu_id=I;
            }
            public int getMenu_id(){
                return menu_id;
            }


        public void readCard(int menu_id){

            try{

                java.sql.Statement statement= con.connection.createStatement();
                this.menu_id=menu_id;

                String gget;

                gget="SELECT * FROM afhaalmenu WHERE menu_id= "+ this.menu_id;

                ResultSet rs= statement.executeQuery(gget);

                rs.next();

                this.menu_id=rs.getInt(1);
                this.name_dish=rs.getString(2);
                this.price= rs.getString(3);
                this.ingredients= rs.getString(4);
                this.recipe= rs.getString(5);
            System.out.println(this.menu_id);               


            }catch(Exception e){
          System.out.println("did not read anything");
            }
        }

        public void updateCard(){

             try{

                 java.sql.Statement statement= con.connection.createStatement();   

                 String sset;
                 sset= "UPDATE afhaalmenu " + "SET name_dish = '" + name_dish + "', " + "price='" + this.price + "'," +
                 " ingredients='" + this.ingredients + "'," + "recipe='" + this.recipe+ "'"+ "WHERE afhaalmenu.menu_id = " + this.menu_id;          

                 statement.executeUpdate(sset);      
                 System.out.println();
             }catch(Exception e){
                 System.out.println("did not update anything");
             }     
        }

        public void writeCard(){
               try{
     java.sql.Statement statement =con.connection.createStatement();

                 String gget;

      gget=  "SELECT "+this.menu_id+" FROM afhaalmenu";


                ResultSet rs =statement.executeQuery(gget);
                rs.last();
                this.menu_id=(rs.getInt(1))+1;

            }catch (Exception e){
                System.out.print("display error");
            con.displaySQLErrors(e);
            }
                            try{

                java.sql.Statement statement= con.connection.createStatement();

                String sset;

                sset= "INSERT INTO afhaalmenu(menu_id, name_dish, price, ingredients, recipe,) VALUES ('" + this.menu_id + "' ,'"+ this.name_dish + "', '"+ this.price +"','"+this.ingredients  + "','" + this.recipe + "');";  
                                    //System.out.println(sset);
                statement.executeUpdate(sset);
                System.out.println(sset);
            }catch (Exception e){

                System.out.println("error writing to database");
                e.printStackTrace();
            }
        }
    @SuppressWarnings("unused")
    public Vector readAllCards(){

            try{

                java.sql.Statement statement= con.connection.createStatement();

                String gget;

                gget="SELECT FROM afhaalmenu; ";                                            
                ResultSet rs= statement.executeQuery(gget);

                while (rs.next()){
                    Vector vRow= new Vector();
                    for(int x=0;x<=3;x++){
                        vRow.addElement(rs.getString(x));
                }
                afhaalKaarten.add(vRow);
            }
            }catch(Exception e){
                //con.displaySQLErrors(e);                  
System.out.println("all cards can not be read");
            }

            return card;
        }
    }
        public Kreta1(){

    con= new Connectie();
    card= new Vector();


    }

public void init(){
    // gui stuff here
    Afhaalmenu1 afm= new Afhaalmenu1(); 
    afm.readCard(1);
    afm.setName_dish("turkse pizza");
    afm.updateCard();
            card=kaart.leesAlleKaarten1();
    System.out.println(kaart.leesAlleKaarten1());
}
public void start(){
    init();
}
public void stop(){

}
public void destroy(){

    }

}

我的连接类,它位于同一个项目中但位于不同的类中(没有从中获取任何错误):

public class Connectie {


    private String host= "localhost";
    private String user= "root";
    private String password="";
    private String databasenaam="kreta";

    java.sql.Connection connection;  

     public void displaySQLErrors( Exception e){
         String error;
         error="SQL foutmelding; "+ e.getMessage();
         JOptionPane.showMessageDialog(null, error, "foutmelding",+
                 JOptionPane.ERROR_MESSAGE);

     }
     public Connectie(){
         try {

             Class.forName("com.mysql.jdbc.Driver").newInstance();
         }
                     catch(Exception e){
             String error= "Niet mogelijk om de driver te laden";
             JOptionPane.showMessageDialog(null, error, "error message",+
                     JOptionPane.ERROR_MESSAGE);
             System.exit(1);
         }
         try{

             connection=DriverManager.getConnection("jdbc:mysql://"+ this.host+ "/"+this.databasenaam+"?user="+ this.user+"&password="+ this.password);

                             String succes= "there is a connectin with database";
             JOptionPane.showMessageDialog(null, succes,"succes",JOptionPane.INFORMATION_MESSAGE);

         }
         catch(Exception e){
             displaySQLErrors(e);                
         } 
     }      
}

这是我在控制台中获得的内容:

1
did not update anything

all cards can not be read
all cards can not be read
[]
1
did not update anything
all cards can not be read
all cards can not be read
[]

我知道我的代码并不完美,因为我不止一次收到错误消息。我只是想弄清楚为什么检索和更新不起作用。干杯

添加e.printStackTrace()时;在我的catch方法中,我得到了这个很长的错误:

1 没有更新任何东西

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 'FROM afhaalmenu' at line 1
All cards can not be read
    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:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2504)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
    at Kreta1$Afhaalmenu1.readAllCards(Kreta1.java:174)
    at Kreta1.init(Kreta1.java:256)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
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 'FROM afhaalmenu' 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:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2504)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
    at Kreta1$Afhaalmenu1.readALLCards(Kreta1.java:174)
    at Kreta1.init(Kreta1.java:257)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
all cards can not be read
[]

0 个答案:

没有答案