The directory is as follows:
Jdbcconnection.java:
Package Jdbc01;import Java.io.inputstream;import Java.sql.connection;import java.sql.driver;import Java.util.properties;import org.junit.test;/** * Decoupling the JDBC connection into the configuration file * @author Sawshaw * */public class jdbcconnection{ public static void Main (string[] args) {}public Connection getconnection () throws Exception{string Driverclass=null; String Jdbcurl=null; String User=null; String Pwd=null;inputstream In=getclass (). getClassLoader (). getResourceAsStream ("jdbc01/sql.properties");// System.out.println ("File Address:" +getclass (). getClassLoader (). GetResource ("jdbc01/sql.properties"));// System.out.println ("File Address:" +getclass (). getClassLoader (). Getsystemresource ("jdbc01/sql.properties")); Properties Properties=new Properties ();p roperties.load (in);d riverclass=properties.getproperty ("Driver"); jdbcurl= Properties.getproperty ("url"), User=properties.getproperty ("user");p wd=properties.getproperty ("pwd");//forname Returns a class that Newinstance creates an object driver driver= (driver) class.forname (driverclass). newinstance (); Properties info=new PrOperties (); Info.put ("User", user), Info.put ("password", pwd); Connection Connection=driver.connect (Jdbcurl, info); return Connection; } @Test public void Testconnection () throws Exception{system.out.println (Getconnection ());}}
Sql.properties:
Driver=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/testuser=rootpwd=root
Pass with JUnit test, connection successful ...
Java Learning----JDBC---Placing database connection information in a configuration file