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

bd96500e110b49cbb3cd949968f18be7.png

I need to connect my MySQL database from within Java program, for that I have to use 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.

How do I proceed with this? How do I connect MySQL database?

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

解决方案

First, you need the mysql jdbc connector. Download the library and add the jar to the classpath.

The next steps (in your application) are to load the jdbc driver and to create a connection:

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.

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值