
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!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Get the Size of HTTP Response using cURL Command (Content Length) - cURL
- Sublime Text 3 Convert Case to Upper, Lower, Title or Swap - Sublime-Text
- Check Bluetooth is turned on or off on Android device programmatically [Java Code] - Android
- How to get an embed code from Vimeo? - HowTos
- Splitting String in Java with Examples - Java
- How to turn off Facebook autoplay videos on timeline - Facebook
- MO229507 UK Europe users cannot access Microsoft 365 - SharePoint, OneDrive, Teams, Admin center, Planner, Yammer - Microsoft
- SharePoint Server 2016 installation System Hardware requirements - SharePoint