Java 如何读取属性文件并连接 MySQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4085420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I read a properties file and connect a MySQL database?
提问by
I need to connect my MySQL database from within Java program, for that I have to use JDBC.
我需要从 Java 程序中连接我的 MySQL 数据库,为此我必须使用 JDBC。
I need to supply it with the necessary connection parameters. And I have to store these parameters in a separate configuration file to be passed as an argument to your Java program during execution. A sample db.properties
file has been provided to me for my reference. The five lines of the file correspond to the host, port, database name, username and password for the database. I need to change the parameters according to your individual system setup.
我需要为它提供必要的连接参数。我必须将这些参数存储在单独的配置文件中,以便在执行期间作为参数传递给 Java 程序。db.properties
已向我提供了一个示例文件供我参考。该文件的五行分别对应于数据库的主机、端口、数据库名称、用户名和密码。我需要根据您的个人系统设置更改参数。
How do I proceed with this? How do I connect MySQL database?
我该如何处理?如何连接MySQL数据库?
basically, I have a createdb.sql file . I have to run that file in mysql. It will create a database. Now I need to populate the database. I have two input files. I need to write a program in java that takes the names of the input data files as command line parameters, parses the files, and populates the data contained within them into your database via JDBC
基本上,我有一个 createdb.sql 文件。我必须在 mysql 中运行该文件。它将创建一个数据库。现在我需要填充数据库。我有两个输入文件。我需要用 java 编写一个程序,它将输入数据文件的名称作为命令行参数,解析文件,并通过 JDBC 将其中包含的数据填充到您的数据库中
采纳答案by Andreas Dolk
First, you need the mysql jdbc connector. Download the library and add the jar to the classpath.
首先,您需要mysql jdbc 连接器。下载库并将 jar 添加到类路径。
The next steps (in your application) are to load the jdbc driverand to create a connection:
下一步(在您的应用程序中)是加载 jdbc 驱动程序并创建连接:
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://[host][:port]/[database]", username, password);
So you'll have to read the config file, extract the values and create the connection string (host, port, database part).
因此,您必须阅读配置文件,提取值并创建连接字符串(主机、端口、数据库部分)。
if you're using eclipse: create a 'lib' folder in your project, copy the jar to that folder, right-click the jar and add it to the build path.
如果您使用的是 eclipse:在您的项目中创建一个“lib”文件夹,将 jar 复制到该文件夹,右键单击该 jar 并将其添加到构建路径中。
if you're executing the application manually, and you did it like this:
如果您手动执行应用程序,并且您是这样做的:
java com.example.MyApplication
do it like this now:
现在就这样做:
java -cp .;path/to/jarfile/connector.jar com.example.MyApplication
(I'm not sure if the lib is named connector.jar, use the correct filename here)
(我不确定 lib 是否命名为 connector.jar,请在此处使用正确的文件名)
Here is tutorial on adding libraries aka "setting the classpath" for java and javac. You need to understand this concept!
这是有关为 java 和 javac 添加库又名“设置类路径”的教程。你需要了解这个概念!
回答by Tomas Narros
You can save you db.properties
file to an external fixed location, and access it for later to retrieve your connection properties:
您可以将db.properties
文件保存到外部固定位置,稍后访问它以检索您的连接属性:
Properties props = new Properties();
FileInputStream in = new FileInputStream("/external/configuration/dir/db.properties");
props.load(in);
in.close();
String driver = props.getProperty("jdbc.driver");
if (driver != null) {
Class.forName(driver) ;
}
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
Connection con = DriverManager.getConnection(url, username, password);
Then, on every environment you can have a different copy of your database settings, without having to change your application file (JAR, ER, or whatever).
然后,在每个环境中,您都可以拥有不同的数据库设置副本,而无需更改应用程序文件(JAR、ER 或其他文件)。
Sample database connection properties file:
示例数据库连接属性文件:
# Oracle DB properties
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1571:MyDbSID
#jdbc.username=root
#jdbc.password=admin
# MySQL DB properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/MyDbName
jdbc.username=root
jdbc.password=admin