我正在使用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位计算机上不工作的原因吗??
感谢
错误告诉您的是JAR中缺少数据库驱动程序类(ODBC驱动程序管理器)。将包含该文件的jar复制到eclipse项目中的/lib文件夹中,然后重新编译jar进行导出,使其包含项目的/lib。
这可能有助于
import java.sql.*;
import java.math.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
/*数据库与MS Access的连接是通过创建此示例中的DataSourceName(dsn)*//*使用此示例的步骤:
- 转到msaccess,制作一个名为"student_base"的表,并给它一个文件名student_base.mdb
- 转到控制面板
- 单击管理工具(windows 2000/xp),单击ODBC(win98)
- 单击ODBC
- 然后,您将看到一个ODBC对话框。单击UserDSn
- 单击"添加"按钮
- 选择Microsoft Access驱动程序(*.mdb)驱动程序,然后单击"完成"
- 提供数据源名称:student_base
- 然后单击选择
- 浏览您创建的数据库名称,然后单击it:studentbase.mdb是一个数据库文件,其中包含所有数据将被存储
单击"确定"。一旦创建了DSN,就可以执行此示例*/
公共类AddNewStudent扩展了JFrame实现ActionListener{
专用JButton btnok、btnext、btnaddnew//按钮专用JTextField tf1、tf2//text字段private JLabel lblname,lbladd,lblmsg//标签
私人JPanel p1,p2,p3,psouth//面板
public AddNewStudent()//构造函数{//初始化按钮btnok=新的JButton("OK");btnok.addActionListener(this);btnext=新JButton("退出");btnext.addActionListener(this);btnaddnew=新JButton("AddNew");btnaddnew.addActionListener(this);
//初始化文本字段tf1=新的JTextField(12);tf2=新的JTextField(12);//初始化标签
lblname=新JLabel("名称:");lbladd=新JLabel("地址:");lblmsg=新JLabel(",JLabel.CENTER);
//initializing panels
p1=新JPanel();p2=新的JPanel();p3=新JPanel();psouth=新JPanel();
//adding buttons and label to panel p1
//setting flowlayout
p1.setLayout(new FlowLayout());
p1.add(btnok);p1.add(btnext);p1.add(btnaddnew);//将lblmsg添加到面板p3p3.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(新GridLayout(3,1));//设置面板p2的线条和标题边框p2.setBorder(BorderFactory.createLineBorder(Color.red));p2.setBorder(BorderFactory.createTitledBborder("输入您的详细信息"));p2.add(lblname);p2添加(tf1);p2添加(lbladd);p2添加(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().equals(")&;(tf2.getText().equals(")){lblmsg.setText("输入您的详细信息");lblmsg.setForeground(颜色.洋红色);}
其他{
尝试{//加载驱动程序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错误");}catch(ClassNotFoundException cnf){System.out.println("找不到类错误");}}}//单击"添加新内容"按钮后,您的文本字段将为空输入
next record
if(e.getSource()==btnaddnew)
{
tf1.setText("");
tf2.setText("");
}
if(e.getSource()==btnexit)
{
System.exit(1);
}
}
}