如何在Java应用程序中设置SQL数据库连接?
我需要用Java制作一个应用程序,它需要使用SQL数据库。我将为用户提供数据库文件,但该文件不会运行,因为要设置数据库连接,我们首先需要在 Windows 中添加用户 DSN。 有没有办法在应用程序安装时自动添加它(用户 DSN)?
I need to make an application in Java , which needs to use SQL database . I wil provide the users with the database file , but that won't run because to setup the database connection we first need to add a User DSN in Windows . Is there a way to add it( User DSN ) automatically when the applcation installs ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Gaurav,执行这些操作的规范方法是使用 JDBC。它们提供统一、简单的界面。
SunOracle 教程Gaurav, the canonical way to do these things is by using JDBC. They provide a uniform, simple interface.
SunOracle tutorial我认为除非有 MySQL 服务器侦听端口 3306,否则这不会起作用。Windows
中的用户 DSN 不是可行的方法。 (这与“平台无关”无关。)
连接到任何数据库的 Java 方式是 JDBC。 下面是一个示例,展示了如何使用 MySQL 进行此操作。当然,您的 CLASSPATH 中需要 Connector-J JAR。
I don't think that'll work unless there's a MySQL server listening on port 3306.
User DSN in Windows is not the way to go. (Nothing "platform independent" about that.)
The Java way to connect to any database is JDBC. Here's an example showing how to do it with MySQL. You'll need the Connector-J JAR in your CLASSPATH, of course.
如果您使用 Java 和 JDBC,则您的数据库连接有一个 URL。
它采用基本形式
jdbc:mysql://hostname:port/database
他们还需要知道您正在安装的数据库的 JDBC 驱动程序类名。因此,您需要提示他们这两件事。
您的用户将需要了解有关其数据库的这些信息才能使用您的应用程序。
您可以按照您的建议使用 JDBC / ODBC 连接器并设置 ODBC Dsname。但如果您想要良好的性能,那么使用本机 JDBC 可能会更好。您可以将驱动程序的类库包含在应用程序类路径中并自行安装。
该驱动程序称为 Connector/J for mySQL,并且运行良好。
If you're using Java and JDBC, your database connection has a URL.
It takes the basic form
jdbc:mysql://hostname:port/database
They're also going to need to know the JDBC driver classname for the database you're installing. So, you'll need to prompt them for those two things.
Your users are going to need to know those things about their data bases to use your app.
You could, as you suggest, use a JDBC / ODBC connector and set up an ODBC Dsname. But if you want good performance, you're probably better off using native JDBC. You can include the class library for the driver in your application class path and install it yourself.
The driver is called Connector / J for mySQL, and it works well.