在Java中为MS Access创建无DSN连接 [英] Creating a DSN-less connection for MS Access within Java

查看:118
本文介绍了在Java中为MS Access创建无DSN连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试为MS-Access创建无DSN连接。我知道如何使用DSN连接访问数据库(转到控制面板--->等等),但我试图连接它而不创建DSN。

这是我的代码: -

  import  java.sql。*; 
public class withoutdsn {

public static void main( String [] args)
{
try
{
Class.forName( sun.jdbc.odbc.JdbcOdbcDriver);
连接con = DriverManager.getConnection( jdbc:odbc:Driver = {Microsoft Access Driver(* .mdb) 。,* ACCDB)}; DBQ = dsn.accdb);
语句st = con.createStatement();
ResultSet rs = st.executeQuery( 插入标签值('Anoop','Delhi') );
}
catch (例外e)
{
System.out.println(e);
}
}
}



如果您在上述代码中发现任何错误,请帮我纠正。

先谢谢

解决方案

您没有在连接字符串中设置文件名。您需要确定数据库文件的位置。这应该是数据库的完整路径。



请参阅 ConnectionStrings.com访问连接字符串 [ ^ ]有关如何格式化连接的详细信息字符串。

Hi I am trying to Create a DSN-less Connection for MS-Access. I know to how connect access database using DSN(go to control panel--->etc etc) but i am trying to connect it without Creating DSN.
Here is my code:-

import java.sql.*;
public class Withoutdsn {

    public static void main(String[] args) 
    {
        try
        {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=dsn.accdb");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("Insert into tab values('Anoop','Delhi')");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}


Please if you find any mistake done in the above code, then help me to correct it.
Thanks in Advance

解决方案

You are not setting the file name in the connection string. You need to identify where the database file is. This should be the full path to the database.

See ConnectionStrings.com Access Connection Strings[^] for more information on how to format the connection string.


这篇关于在Java中为MS Access创建无DSN连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆