无法使用Java 64位连接到MS Access 2010 64位
我正在使用Eclipse64位开发一个连接到MSAccess201064位的解决方案 当我使用eclipse运行这个项目时,这个连接工作得非常好。(连接到数据库并执行插入、更新和删除操作)。但是,当我将它导出为可执行的*.jar文件并运行它时,它会给我这个错误无法使用Java 64位连接到MS Access 2010 64位,java,Java,我正在使用Eclipse64位开发一个连接到MSAccess201064位的解决方案 当我使用eclipse运行这个项目时,这个连接工作得非常好。(连接到数据库并执行插入、更新和删除操作)。但是,当我将它导出为可执行的*.jar文件并运行它时,它会给我这个错误 java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 为什么
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)*/
/*使用此示例的步骤:
- 转到ms access,制作一个名为“student_base”的表格,并给它一个 文件名student_base.mdb
- 转到控制面板
- 单击管理工具(windows 2000/xp),单击 ODBC(win98)
- 点击ODBC
- 然后,您将看到一个ODBC对话框。点击UserDSn
- 点击添加按钮
- 选择Microsoft Access驱动程序(*.mdb)驱动程序并单击“完成”
- 提供数据源名称:student_base
- 然后点击选择
- 浏览已创建的数据库名称,然后单击 it:student_base.mdb是一个数据库文件,其中包含所有数据 将存储
- 点击OK。 创建DSN后,可以执行此示例*/ 公共类AddNewStudent扩展JFrame实现ActionListener{ 私有JButton btnok、btnexit、btnaddnew;//按钮 私有JTextField tf1,tf2;//textfields 私有JLabel lblname,lbladd,lblmsg;//标签 私有JPanel p1、p2、p3、psouth;//面板 public AddNewStudent()//构造函数 { //初始化按钮 btnok=新的JButton(“OK”); btnok.addActionListener(这个); btnexit=新的JButton(“退出”); btnexit.addActionListener(这个); btnaddnew=新的JButton(“AddNew”); btnaddnew.addActionListener(这个) //初始化文本字段 tf1=新的JTextField(12); tf2=新的JTextField(12); //初始化标签
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
if((tf1.getText().equals(“”)和((tf2.getText().equals(“”))之间)
{
lblmsg.setText(“输入您的详细信息”);
lblmsg.setForeground(颜色为洋红色);
}
否则
{
试一试
{
//加载驱动程序
forName(“sun.jdbc.odbc.JdbcOdbcDriver”)
发送到数据库
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();
}
捕获(SQLException sqe)
{
System.out.println(“SQl错误”);
}
捕获(ClassNotFoundException cnf)
{
System.out.println(“未找到类错误”);
}
}
}
//单击“添加新”按钮后,您的文本字段将为空
进入
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);String database=“jdbc:odbc:Driver={Microsoft Access Driver(*.mdb,*.accdb)};DBQ=“+fileName;conn=DriverManager.getConnection(数据库,“测试”,“sa”);这是我的连接字符串,您希望我添加哪个jar文件请帮助我没有想到您使用了内部sun的JDBC驱动程序,我以为您使用了来自外部jar的JDBC驱动程序(至少我这样做了)。我的想法是,Eclipse中的JDK有sun.*类,因此它可以运行您的应用程序,但操作系统上的JRE在其sun.*包中没有特定的JDBC类,因此它无法运行。您是否可以尝试从外部链接下载JDBC驱动程序作为单独的jar进行访问,然后将该jar添加到您的应用程序下project的/lib,加载该类而不是sun的类,然后将项目导出到可执行的jar中?这如何解释它为什么不能在64位机器上工作?是否在32位机器上检查过它?
//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
//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
//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();
next record
if(e.getSource()==btnaddnew)
{
tf1.setText("");
tf2.setText("");
}
if(e.getSource()==btnexit)
{
System.exit(1);
}
}
}