我正在使用Eclipse 64位开发一个连接到Ms Access 2010 64位的解决方案。
当我使用eclipse运行这个项目时,这种连接非常有效。 (是否连接到数据库并进行插入,更新和删除)。但是,当我将其作为可执行* .jar文件导出并运行它时,它会给我这个错误
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
为什么它在eclipse中有效并且不能用作jar文件?
此外,如果我将此jar文件复制到32位计算机并使用jre-7u6-windows-i586
java运行时执行此jar文件,它将完美运行。
有人知道它在32位计算机上运行的原因而不是64位计算机吗?
由于
答案 0 :(得分:0)
错误告诉您的是您在JAR中缺少数据库驱动程序类(ODBC驱动程序管理器)。将具有该文件的jar复制到eclipse项目中的/ lib文件夹中,然后重新编译jar以进行导出,使其包含项目的/ lib。
答案 1 :(得分:0)
这可能对您有所帮助
import java.sql.*;
import java.math.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
/ *与MS-Access的数据库连接通过创建完成 此示例中的DataSourceName(dsn)* / / *使用此示例的步骤:
单击“确定”。 创建DSN后,您可以执行此示例* /
public class AddNewStudent extends JFrame实现了ActionListener {
私人JButton btnok,btnexit,btnaddnew; //纽扣 private JTextField tf1,tf2; // textfields 私人JLabel lblname,lbladd,lblmsg; //标签
私人JPanel p1,p2,p3,psouth; //面板
public AddNewStudent()//构造函数 { //初始化按钮 btnok =新的JButton(“OK”); btnok.addActionListener(本); btnexit = new JButton(“退出”); btnexit.addActionListener(本); btnaddnew =新的JButton(“AddNew”); btnaddnew.addActionListener(本);
//初始化文本字段 tf1 = new JTextField(12); tf2 = new JTextField(12); //初始化标签
lblname = new JLabel(“Name:”); lbladd = new JLabel(“地址:”); lblmsg = new JLabel(“”,JLabel.CENTER);
//initializing panels
p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); psouth = new JPanel();
//adding buttons and label to panel p1
//setting flowlayout
p1.setLayout(new FlowLayout());
p1.add(btnok); p1.add(btnexit); p1.add(btnaddnew); //将lblmsg添加到面板p3 p3.add(lblmsg);
//adding both the panels to new panel,psouth
//settin layout 2:1
psouth.setLayout(new GridLayout(2,1));
psouth.add(p3);
psouth.add(p1);
//adding label and textfields to panel p2
p2.setLayout(new GridLayout(3,1)); //面板p2的设置线和标题边框 p2.setBorder(BorderFactory.createLineBorder(Color.red)); p2.setBorder(BorderFactory.createTitledBorder(“输入您的详细信息”)); p2.add(lblname); p2.add(TF1); p2.add(lbladd); p2.add(TF2);
//adding panel to container
this.getContentPane().add(p2,"Center");
this.getContentPane().add(psouth,"South");
this.setSize(300,300);
this.setLocation(100,200);
this.show();
}
public static void main(String args[])
{
AddNewStudent ad = new AddNewStudent();
}
//事件处理
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnok)
{
PreparedStatement pstm;
ResultSet rs;
String sql;
//if no entries has been made and hit ok button throw an error
//you can do this step using try clause as well
如果((tf1.getText()等于( “”)及;&安培;(tf2.getText()等于( “”)))) { lblmsg.setText(“输入您的详细信息”); lblmsg.setForeground(Color.magenta); }
否则 {
试 { //加载驱动程序 的Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver中”);
//connection object created using DriverManager class
//student_base is the name of the database
Connection connect =
DriverManager.getConnection("jdbc:odbc:student_base");
//creating prepared statement object pstm so that query can be
发送到数据库
pstm=connect.prepareStatement("insert into student_base
values(?,?)");
pstm.setString(1,tf1.getText());
pstm.setString(2,tf2.getText());
//execute method to execute the query
pstm.executeUpdate();
lblmsg.setText("Details have been added to database");
//closing the prepared statement and connection object
pstm.close();
connect.close();
} catch(SQLException sqe) { System.out.println(“SQl error”); } catch(ClassNotFoundException cnf) { System.out.println(“未找到类错误”); } } } //点击按钮addnew后,你的文本字段将为空 输入
next record
if(e.getSource()==btnaddnew)
{
tf1.setText("");
tf2.setText("");
}
if(e.getSource()==btnexit)
{
System.exit(1);
}
}
}