实例1:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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 JDBC {
private static Properties pro=new Properties();
static{
try {
//getClassLoader() 动态加载类到JVM 中
pro.load(new FileReader(JDBC.class.getClassLoader()
.getResource("jdbc.properties").getPath()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn=null;
try {
Class.forName(pro.getProperty("Drivername"));
conn=DriverManager.getConnection(
pro.getProperty("url"),
pro.getProperty("username"),
pro.getProperty("password"));
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
实例2:
import java.io.IOException;
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 JDBC2 {
public static String driverClassName;
public static String url;
public static String username;
public static String password;
static {
Properties properties = new Properties();
try {
properties.load(JDBC2.class.getClassLoader().getResourceAsStream("jdbc.properties"));
driverClassName=properties.getProperty("Drivername");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getconnection() {
Connection conn = null;
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
实例3:
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
public class JDBC3 {
private static BasicDataSource dataSource;
static {
Properties properties = new Properties();
InputStream inputStream = JDBC3.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(inputStream);
inputStream.close();
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getconnection() throws SQLException{
return dataSource.getConnection();
}
}
注意如果如下错误,解决方案:导入 commons-logging的jar包
注意如果如下错误,解决方案:导入 commons-pool2的jar包
即使用下面依赖中的三个jar包:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.6.0</version>
</dependency>
jdbc.properties 文件如下:
Drivername=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/shujuku
username=root
password=123456