Java中的SQL发送到两个表但有错误
*******************************************由I-LOVE-2-RESOVE解决,请参见下文************************** 数据库在添加“fileNo”作为其他默认字段时抛出错误,其中dates和defualt设置为0。所以在access中,默认设置了=Date(),错误得到了解决。再次感谢大家 *************************************************************************解决方案结束******************************************** 我有一些严重的问题,我的大脑已经完全融化在我试图解决它 基本上,我的access数据库中有两个表,一个“Files”表,包含FileNo、TrailDate、Clientname。 另一个叫做“待办事项”的 文件编号1、1a、1b、2、2a、2b等。(1涉及你需要做的事情,1a涉及需要完成的日期) 然后我的java程序有两个入口点。 你输入一个文件。这将发送到文件数据库,工作正常! 第二个将“FileNo”列从“Files”拉到Jcombo框中,单击该文件号后,在“ToDO”表中搜索1,1a、2,2a等中的数据。然后将其输出,以便用户可以编辑或查看 我现在的问题是我需要它,这样当你在AddFile GUI中输入一个新文件,然后将该文件也添加到“ToDO”数据库中 这是我正在使用的代码示例Java中的SQL发送到两个表但有错误,java,sql,Java,Sql,*******************************************由I-LOVE-2-RESOVE解决,请参见下文************************** 数据库在添加“fileNo”作为其他默认字段时抛出错误,其中dates和defualt设置为0。所以在access中,默认设置了=Date(),错误得到了解决。再次感谢大家 *********************************************************************
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”表中的文件号是长文本的,正如“Files”表中的文件号一样,我对文件输入没有任何问题,但当我尝试向TodO添加相同的数据时,我得到了错误
我是一个相当新的Java,这是我的第一个完整项目,因此,如果这是一个琐碎的问题,但我搜索了又搜索,并且我找不到适合我的问题的答案,我深表歉意:(
……编辑
这是该方法的所有代码
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();
}
});
}
在上面请注意,我有两个按钮做同样的事情。B和B1通常有一个GUI退出,作为保存和退出类的交易。
这段代码中可能有一些miner错误,请忽略这些错误,因为在我出门工作并发布到这里之前,我已经很快地对其进行了编辑
再次感谢您的查找:)此错误源于日期字段的默认值无效。MsAccess无法将0识别为有效日期,并导致类型转换错误。使用
default=Date()
一个有效的日期或没有默认值来修复您确定要清理文件名吗?它是否包含“如果是,那么你需要逃避它我还想知道是否值得我用另一种方法来比较FileNo上的两个表。如果ToDO没有一个要添加的文件号。但遗憾的是,我在SQL代码方面并没有那么先进,无法理解如何做到这一点!Pratik,文件号取自方法前面的Jtextfield(String fileNumber=t.getText();“t是textfield”),正如我所说,这对文件SQL很有效,我使用完全相同的字符串发送给ToDO,所以我看不出这怎么会有问题?@Chris_livermore你能告诉我们全部错误吗?ToDo表中的其余字段都可以为空吗?Try run(“插入到ToDO(FileNo,1,1a,1b,2,2a,2b等)值(“+fileNumber+”,NULL,NULL,NULL,NULL,NULL,NULL)”)@I-LOVE-2-reserve,literally是完整的错误,这只是一件事,ToDO中的所有其他字段都可以为NULL,我唯一能想到的是我确实有数据库输入一个设置的日期,这只是对默认值的0调用。我会发布上面的全部代码,但请注意,它真的很混乱!