gpt4 book ai didi

java - Java 中的 MS Access 连接

转载 作者:行者123 更新时间:2023-12-02 07:02:17 25 4
gpt4 key购买 nike

我正在用java编写一个用于抽认卡的程序。我有一个启动主JFrame的init类。从那里我有按钮可以访问框架来执行添加卡片、学习和帮助等任务。我编写了用于添加卡片的 AddCards 类。在此我尝试连接到 MS Acess 数据库以在其中存储卡片,但我的程序返回“错误”。我找不到错误。请帮我指出它。我正在 NetBeans 中编写代码。我的 AddCards 代码:

  package fc;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.*;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AddCards implements ActionListener
{
JFrame f1=new JFrame("Add Cards");
JLabel l1=new JLabel("Enter question");
JLabel l2=new JLabel("Enter answer");
JTextField t1=new JTextField();
JTextField t2=new JTextField();
JButton b1=new JButton("ADD");
JButton b2=new JButton("CLEAR");
String ques,ans;
Statement st;


AddCards() throws SQLException
{
f1.setSize(600,500);
f1.setLayout(null);
f1.add(l1);
f1.add(l2);
f1.add(t1);
f1.add(t2);
f1.add(b1);
f1.add(b2);
l1.setBounds(70, 60, 100,20);
t1.setBounds(170, 60, 300, 100);
l2.setBounds(70, 200, 100,20);
t2.setBounds(170, 200, 300, 100);
b1.setBounds(200, 350, 100, 30);
b2.setBounds(350, 350, 100, 30);
f1.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}


public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
Connection con=null;
Statement st=null;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:fc");

ques=t1.getText();
ans=t2.getText();
String addRow="INSERT INTO data(ques,ans) values('"+ques+"','"+ans+")";
st.executeUpdate(addRow);
con.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(AddCards.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException ex)
{
System.out.println("Error");
}
}
if(e.getSource()==b2)
{
t1.setText(" ");
t2.setText(" ");
}

}
}

使用堆栈跟踪后生成的整个错误如下:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at fc.AddCards.actionPerformed(AddCards.java:58)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

最佳答案

System.out.println("Error"); 替换为 ex.printStackTrace();

Logger.getLogger(AddCards.class).log(Level.SEVERE, "SQL exception while inserting into data", ex);

,您将获得有用的错误消息和堆栈跟踪,让您知道出了什么问题。

但是错误很明显:您忘记了查询中的单引号:

String addRow="INSERT INTO data(ques,ans) values('"+ques+"','"+ans+")"
here --^

您还应该学习使用prepared statements ,这将避免这种错误,不会使您的代码成为 SQL injection attacks 的主题,并且如果问题或答案包含单引号,也不会导致您的代码中断。

关于java - Java 中的 MS Access 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16498775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com