Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Create JDBC connection using properties file

 
package com.javaartifacts;
 
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
 
public class TestConnection {
 
	public static Properties loadPropertiesFile() throws Exception {
 
		Properties prop = new Properties();
		InputStream in = new FileInputStream("jdbc.properties");
		prop.load(in);
		in.close();
		return prop;
	}
 
	public static void main(String[] args) {
 
		System.out.println("create jdbc connection using properties file");
		
		Connection con = null;
 
		try {
 
		Properties prop = loadPropertiesFile();
 
		String driverClass = prop.getProperty("MYSQLJDBC.driver");
		String url = prop.getProperty("MYSQLJDBC.url");
		String username = prop.getProperty("MYSQLJDBC.username");
		String password = prop.getProperty("MYSQLJDBC.password");
 
		Class.forName(driverClass);
 
		con = DriverManager.getConnection(url, username, password);
 
		if (con != null) {
		System.out.println("connection created successfully using properties file");
		}
 
		else {
		System.out.println(" unable to create connection");
		}
 
		}catch (SQLException e) {
		e.printStackTrace();
		} 
		catch (Exception e) {
		e.printStackTrace();
		} finally {
 
		try {
		if (con != null) {
		con.close();
		}
		} catch (Exception ex) {
		ex.printStackTrace();
		}
		}
	}
 
}
 
Comment

PREVIOUS NEXT
Code Example
Java :: bukkit java set leather armor color from hex 
Java :: maven skip make-assembly 
Java :: hasNext for scanner class java 
Java :: resizing ImageIcon in JButton java 
Java :: finding length of arrays in java 
Java :: java stream inner join two lists 
Java :: java benchmark time 
Java :: add infinite numbers to variable java 
Java :: java calcuate milliseconds since 1970 
Java :: android studio Toast usage 
Java :: Java Enabling Assertions 
Java :: generate random number using math.random in java 
Java :: java create an instance of a stack 
Java :: object type in java 
Java :: java Detect Cycle in a Directed Graph 
Java :: how to make a button disapear on click in javafx 
Java :: list of strings java 
Java :: how to create search function in android studio 
Java :: what is arraylist 
Java :: 9999999999999 
Java :: split string to textview in android 
Java :: all possible substrings of a string java of specific length 
Java :: spring-boot-maven-plugin not found 
Java :: maths.random in Java 
Java :: merge without extra space 
Java :: get field name java 
Java :: index 1 out of bound for length 1 java 
Java :: hashtable in java 
Java :: Java Remove HashMap Elements 
Java :: get ocurrences in array java 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =