
It is always better to keep the configuration details in the properties file, this helps you choose the Database and user details based on the environment like local/staging/uat/production.
Step 1: Create a local_configuration.properties file
local_configuration.propertiesMYSQL_JDBC_DRIVER_CLASS=com.mysql.cj.jdbc.Driver
MYSQL_DB_URL=jdbc:mysql://localhost:3306/my_uat
MYSQL_DB_USER=root
MYSQL_DB_USER_PASSWORD=root123
Step 2: Reading the Properties File
InputStream inputStream = new FileInputStream("local_configuration.properties");
Properties properties = new Properties();
properties.load(inputStream);
String url = properties.getProperty("MYSQL_DB_URL");
String user = properties.getProperty("MYSQL_DB_USER");
String password = properties.getProperty("MYSQL_DB_USER_PASSWORD");
Step 3: Complete JDBC Code
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCReadFromPropertiesFile {
public static void main(String[] args) throws SQLException, IOException {
InputStream inputStream = new FileInputStream("local_configuration.properties");
Properties properties = new Properties();
properties.load(inputStream);
String url = properties.getProperty("MYSQL_DB_URL");
String user = properties.getProperty("MYSQL_DB_USER");
String password = properties.getProperty("MYSQL_DB_USER_PASSWORD");
String sql = "Select * from students";
Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()) {
System.out.print(resultSet.getInt(1)+"\t");
System.out.print(resultSet.getString(2)+"\t");
System.out.print(resultSet.getDate(3)+"\t");
System.out.println();
}
}
}
Output:
1 Mike 2001-07-22
2 Alex 2002-08-13
3 Sam 2022-01-25
-
Have Questions? Post them here!
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- How to check your IP using bash for Windows? - Bash
- How to know the Docker Engine Version - Docker
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5 - jQuery
- Microsoft Sign-in Error Code: 50058 (Request Id, Correlation Id and Timestamp) - Microsoft
- Instant Run requires Tools | Android | Enable ADB integration to be enabled - Android-Studio
- Bootstrap Button Colors Classes - Bootstrap
- Java java.time.Clock class code examples [Java Date Time API] - Java
- ls command sort by file size [Linix/UNIX/macOS/bash] - Linux