简体   繁体   中英

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.

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

You can save you db.properties file to an external fixed location, and access it for later to retrieve your connection 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).

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 . 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)

Here is tutorial on adding libraries aka "setting the classpath" for java and javac . You need to understand this concept!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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