Updated October 2026. Every example was run on Java 21 against MariaDB 10.11 with MySQL Connector/J 9.7.
A JDBC connection with a properties file keeps the URL, user name and password outside your Java code. You load them with java.util.Properties at startup, so you can change a database without recompiling. First, this post shows the full working code. Then it covers the mistakes that break it.
Step 1: Create the properties file
Create a file called db.properties. Each line is a key=value pair.
db.url=jdbc:mysql://localhost:3306/jdbcdemo
db.user=demo
db.password=demo123
Three keys are enough. In particular, you do not need a driver class name, because DriverManager finds the driver on its own from the jar on your classpath (since JDBC 4.0).
The test table for the examples below has three rows:
id name city
101 Rajesh Hyderabad
102 Anita Bangalore
103 Chandra Hyderabad
Step 2: Load the file and connect
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class PropsConnection {
public static void main(String[] args) throws IOException, SQLException {
Properties props = new Properties();
try (FileInputStream in = new FileInputStream("app/db.properties")) {
props.load(in);
}
String url = props.getProperty("db.url");
String user = props.getProperty("db.user");
String password = props.getProperty("db.password");
String sql = "SELECT id, name, city FROM student WHERE city = ? ORDER BY id";
try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement ps = con.prepareStatement(sql)) {
ps.setString(1, "Hyderabad");
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name")
+ " " + rs.getString("city"));
}
}
}
}
}
Run it with the connector jar on the classpath:
java -cp mysql-connector-j-9.7.0.jar PropsConnection.java
Output:
101 Rajesh Hyderabad
103 Chandra Hyderabad
Here is what the code does. First, the pieces:
Properties.load()reads thekey=valuelines into a map-like object.try-with-resourcescloses the file, the connection, the statement and the result set for you, even when an exception happens.PreparedStatementwith a?placeholder is safer than building the SQL with string concatenation. It also protects you from SQL injection.
For more on prepared statements, see the JDBC PreparedStatement example.
Load the file from the classpath instead
new FileInputStream("app/db.properties") depends on the folder where you start the program. That works on your laptop, but it often breaks once the code runs from a jar or an application server. Instead, put the file on the classpath and load it by name.
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ClasspathProps {
static Properties load(String name) throws IOException {
Properties props = new Properties();
try (InputStream in = ClasspathProps.class.getClassLoader().getResourceAsStream(name)) {
if (in == null) {
throw new IOException(name + " not found on the classpath");
}
props.load(in);
}
return props;
}
public static void main(String[] args) throws IOException, SQLException {
Properties props = load("db.properties");
// an environment variable wins over the file, so secrets stay out of source control
String password = System.getenv().getOrDefault("DB_PASSWORD", props.getProperty("db.password"));
try (Connection con = DriverManager.getConnection(
props.getProperty("db.url"), props.getProperty("db.user"), password)) {
System.out.println("Connected to " + con.getCatalog());
}
}
}
Output:
Connected to jdbcdemo
In a Maven or Gradle project, put the file in src/main/resources. That way, the build copies it onto the classpath for you.
The environment variable line matters. If DB_PASSWORD is set, it replaces the password from the file. So you can commit a db.properties with a harmless default, and the real password stays on the server only.
Common errors and how to read them
1. The old driver name. Many older tutorials, including the first version of this post, use sun.jdbc.odbc.JdbcOdbcDriver. That bridge driver was removed in Java 8, so it fails today:
Exception in thread "main" java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
To fix it, remove the Class.forName(...) call and the driver key, because modern drivers register themselves.
2. The file is not found. If you start the program from a different folder, FileInputStream fails:
Exception in thread "main" java.io.FileNotFoundException: db.properties (No such file or directory)
To fix it, check your working directory, or switch to the classpath version above.
3. A wrong password. The connection fails with SQL state 28000:
SQLState: 28000
Message : Access denied for user 'demo'@'localhost' (using password: YES)
First, check the user and password in the file. Then check that the database user is allowed to connect from your host.
Common mistakes
- Committing the password to Git. Instead, add
db.propertiesto.gitignore, or read the password from an environment variable as shown above. - Forgetting to close the file. An unclosed
FileInputStreamleaks a file handle. Usetry-with-resources. - Closing the connection only on the success path. If an exception occurs before
con.close(), the connection stays open. Again,try-with-resourcesfixes this. - Using a
Statementwith string concatenation. Instead, usePreparedStatementand?placeholders. - Mixing a MySQL URL with another driver. The
jdbc:mysql://URL needs a MySQL (or compatible) driver jar on the classpath.
Summary
To open a JDBC connection with a properties file, you load db.properties with Properties.load(), read the URL, user and password, and pass them to DriverManager.getConnection(). In short, load the file from the classpath, close everything with try-with-resources, and keep real passwords out of your repository.
References
Related posts
- Step by Step JDBC Program Example
- JDBC PreparedStatement Example Program
- Transaction Management in JDBC Example
Thanks!