************************************由 I-LOVE-2-REVIVE 解决 请参见下文******************
数据库在添加“fileNo”作为其他字段时返回错误,这些字段进入默认值,其中日期和默认值设置为 0。因此在 access =Date() 中设置为默认值,错误已解决。再次感谢全部!
<强>****************************************************结束解决方案********************************************
我遇到了一些严重的问题,我的大脑已经完全融化在我试图解决它的过程中!
所以基本上我的访问数据库中有 2 个表,一个"file"表包含 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,因为它在"file"表中,我对文件输入完全没有问题,但第二次我尝试将相同的数据添加到 ToDO 时,我收到错误! p>
我是一个相当新的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();
}
});
}
在上面请注意我有 2 个按钮做同样的事情。 B 和 B1 B1 通常有一个 GUI 退出,作为保存和退出的处理。 这段代码中可能存在一些矿工错误,请忽略这些错误,因为我在出门工作并发布到这里之前很快就对其进行了编辑。
再次感谢您的浏览:)
最佳答案
此错误是由于日期字段的默认值无效造成的。 MsAccess 无法将 0 识别为有效日期并导致类型转换错误。使用default =Date()
有效日期或无默认值来修复
关于java - Java 中的 SQL 发送到两个表但有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24696980/