繁体   English   中英

Java中的SQL发送到两个表但有错误

[英]SQL in Java sending to two Tables but having errors

**************************************由I-LOVE-2-REVIVE解决如下************

数据库正在抛出错误wen,并添加了一个“ fileNo”作为其他字段,这些其他字段默认设置为date和defualt设置为0。因此,在access中默认设置= Date()并解决了Error。再次感谢所有!

***********************************************解决方案结束**** ***********************************

即时通讯遇到了严重的麻烦,而我的大脑完全无法解决问题!

所以基本上我在Access数据库中有2个表,一个包含FileNo,TrailDate,Clientname的“文件”表。 另一个称为“ ToDO”的文件编号为1,1a,1b,2,2a,2b ECT。 (1与您需要做的事情有关,而1a与需要完成的日期有关)

然后我在Java程序上有2个入口点。 一个你输入一个文件。 这将发送到文件数据库并正常工作! 第二个将“ FileNo”列从“ Files”拉至Jcombo框,然后单击该文件,Number在“ ToDO”表中搜索1,1a,2,2a等中的数据。 然后输出此内容,以便用户可以编辑或查看。

我现在的问题是我需要它,以便当您在AddFile GUI中输入一个新文件,然后将该FileNo也添加到“ ToDO”数据库中时。

这是我正在使用的代码示例

try { //// WORKING  
       PreparedStatement statement = (PreparedStatement)connect.prepareStatement("INSERT INTO Files(FileNo,TrialDate,ClientName)VALUES('"+fileNumber+"','"+trialDate+"','"+clientName+"')");
            statement.executeUpdate();
            statement.close();
            connect.close();
            JOptionPane.showMessageDialog(null, "File added to Database. Returning to main menu");
            System.out.println("Success!");
            adfFrame.setVisible(false);
            mainMenu();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, "File name already in Database!");
            System.out.println("Error" + ex);}

现在,当我将此代码更改为:

try { 
       PreparedStatement statement = (PreparedStatement)connect.prepareStatement("INSERT INTO ToDO(FileNo)VALUES('"+fileNumber+"')");
                            statement.executeUpdate();
            statement.close();
            connect.close();
            JOptionPane.showMessageDialog(null, "File added to Database. Returning to main menu");
            System.out.println("Success!");
            adfFrame.setVisible(false);
            mainMenu();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, "File name already in Database!");
            System.out.println("Error" + ex);}

我得到一个SQL异常读取:net.ucanaccess.jdbc.UcanaccessSQLException:转换中的数据类型不兼容

但问题是我不知道怎么可能!! 如在我的数据库设计中一样,“ TodO”表中的FileNo是LongText,正如“文件”表中的一样,我是LongText,但我输入的文件完全没有问题,但是第二次我尝试将相同的数据添加到ToDO中时出现错误!

我是一个相当新的Java,这是我的第一个完整项目,因此,如果这很琐碎,但我进行了iv搜索和搜索,并且我找不到适合我的问题的合适答案,抱歉:(

.......编辑....

这是该方法的所有代码。

   private void fileFrame() {    
    //POPULATES CLIENTS STRING ARRAY 

    String[] clients= new String[noclients];
    String sql1 = "SELECT ClientName FROM ClientTable";
        try {
            rs = st.executeQuery(sql1);
        } catch (SQLException ex) {
            Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
        }
    System.out.println(sql1);
    int count= 0;           
        try {
            while (rs.next()){
                clients[count] = rs.getString("ClientName");
                count = count + 1; 
                System.out.println("Clients="+clients);
            }   } catch (SQLException ex) {
            Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
        }
//FINISHES CLIENT ARRAY POPULATION.        
    JLabel l = new JLabel("File Reference");
    JLabel l1 = new JLabel("Trial Date");
    JTextField t = new JTextField(10);
    JTextField t1 = new JTextField(10);
    JComboBox clientList = new JComboBox(clients);

    JButton b = new JButton("Save");
    JButton b1 = new JButton("Save and Exit");



        adfFrame.setSize(500,800);
        adfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        adfFrame.setLocation(700, 500);
        adfFrame.setVisible(true);
        //Adds TExt Fields/Labels/Button to the GUI
        JPanel p = new JPanel();
        p.add(l);
        p.add(t);
        p.add(l1);
        p.add(t1);
        p.add(clientList);
        p.add(b);
        p.add(b1);

        adfFrame.add(p);     
        p.getRootPane().setDefaultButton(b);

        //save button pressed
        b.addActionListener(new ActionListener() {


            @Override
        public void actionPerformed(ActionEvent e) {
            String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
            }
            //##DECLARES FILEPATH##
            String filePath = "C://Users/Chris/java/Tutorials GUI/2.Login page with Database/Login1/Login1.accdb";

            //##PULL FILEPATH FROM ABOVE##
            String db = "jdbc:ucanaccess://"+filePath;
            Connection connect = null;
            try {
                connect = DriverManager.getConnection(db);
                /// CONNECTION  MADE
            } catch (SQLException ex) {
                Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
            }
            String fileNumber=t.getText();
            //System.out.println(fileNumber);
            String clientName=clientList.getSelectedItem().toString();
            //System.out.println(clientName);
            String trialDate =t1.getText();
            //System.out.println(trialDate);


            try { //// WORKING  
           PreparedStatement statement = (PreparedStatement)connect.prepareStatement("INSERT INTO Files(FileNo,TrialDate,ClientName)VALUES('"+fileNumber+"','"+trialDate+"','"+clientName+"')");
          System.out.println("1");
           statement.executeUpdate(); 
           System.out.println(statement);
           statement.close();
                //statement.setString(1,clientName);
                //statement.setString(2,yn);
           //PreparedStatement stotement = (PreparedStatement)connect.prepareStatement("INSERT INTO ToDO [FileNo] VALUES('"+fileNumber+"')");
          //System.out.println("2");
          //stotement.executeUpdate(); System.out.println(stotement);
               // stotement.close();
                connect.close();
                System.out.println("Success!");

            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "File already in Database!");
                System.out.println("Error" + ex);
            }



            t.setText("");
            t1.setText("");
            fcount();
        }
    });

        //Save and exit button pressed
        b1.addActionListener(new ActionListener() {


            @Override
        public void actionPerformed(ActionEvent e) {
            String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
            }
            //##DECLARES FILEPATH##
            String filePath = "C://Users/Chris/java/Tutorials GUI/2.Login page with Database/Login1/Login1.accdb";

            //##PULL FILEPATH FROM ABOVE##
            String db = "jdbc:ucanaccess://"+filePath;
            Connection connect = null;
            try {
                connect = DriverManager.getConnection(db);
                /// CONNECTION  MADE
            } catch (SQLException ex) {
                Logger.getLogger(Login1.class.getName()).log(Level.SEVERE, null, ex);
            }
            String fileNumber=t.getText();
            //System.out.println(fileNumber);
            String clientName=clientList.getSelectedItem().toString();
            //System.out.println(clientName);
            String trialDate =t1.getText();
            //System.out.println(trialDate);


            try { //// WORKING  
           PreparedStatement statement = (PreparedStatement)connect.prepareStatement("INSERT INTO Files(FileNo,TrialDate,ClientName)VALUES('"+fileNumber+"','"+trialDate+"','"+clientName+"')");

                statement.executeUpdate();
                statement.close();
                connect.close();
                JOptionPane.showMessageDialog(null, "File added to Database. Returning to main menu");
                System.out.println("Success!");

            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "File name already in Database!");
                System.out.println("Error" + ex);
                adfFrame.setVisible(false);
                mainMenu();


            }

try { 
           PreparedStatement statement = (PreparedStatement)connect.prepareStatement("INSERT INTO ToDO(FileNo)VALUES('"+fileNumber+"')");
                statement.executeUpdate();
                statement.close();
                connect.close();
                JOptionPane.showMessageDialog(null, "File added to Database. Returning to main menu");
                System.out.println("Success!");

            }catch(Exception ex){
                JOptionPane.showMessageDialog(null, "File name already in Database!");
                System.out.println("Error" + ex);

         }

            t.setText("");
            t1.setText("");
            fcount();

        }
    });

}

在上面请注意,我有2个按钮在做同样的事情。 B和B1 B1通常在其上面有一个GUI出口,作为保存和退出之类的交易。 这段代码中可能存在一些矿工错误,请忽略这些错误,因为我已经迅速对其进行了编辑,因为我跑出工作地点并贴在这里。

再次感谢您的光临:)

此错误来自日期字段的默认值无效。 MsAccess无法将0识别为有效日期,并导致类型转换错误。 使用default =Date()有效日期或不使用默认值进行修复

暂无
暂无

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

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