简体   繁体   English

如何读取属性文件并连接MySQL数据库?

[英]How do I read a properties file and connect a MySQL database?

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. 我必须将这些参数存储在一个单独的配置文件中,以便在执行期间作为参数传递给Java程序。 A sample db.properties file has been provided to me for my reference. 我已经提供了一个示例db.properties文件供我参考。 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. 我需要根据您的个人系统设置更改参数。

How do I proceed with this? 我该如何处理? How do I connect MySQL database? 如何连接MySQL数据库?

basically, I have a createdb.sql file . 基本上,我有一个createdb.sql文件。 I have to run that file in mysql. 我必须在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 我需要在java中编写一个程序,它将输入数据文件的名称作为命令行参数,解析文件,并通过JDBC将包含在其中的数据填充到数据库中

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

First, you need the mysql jdbc connector . 首先,您需要mysql jdbc连接器 Download the library and add the jar to the classpath. 下载库并将jar添加到类路径中。

The next steps (in your application) are to load the jdbc driver and 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 . 这是关于添加库的教程,也就是为java和javac设置“设置类路径” You need to understand this concept! 你需要了解这个概念!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM