java 获取数据库连接的实现代码
程序员文章站
2023-12-18 08:06:34
代码如下所示:复制代码 代码如下:/***connectionutil .java***/package com.cai.jdbc;import java.sql.conn...
代码如下所示:
/**
*connectionutil .java
*
*
*/
package com.cai.jdbc;
import java.sql.connection;
import java.sql.drivermanager;
import java.util.properties ;
public class connectionutil {
/**
* 1
*在方法中固化连接参数
* @return 数据库连接
*/
public connection getconnection(){
connection conn = null ;
try{
class.forname("com.mysql.jdbc.driver") ;
conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/stud", "root", "root") ;
return conn ;
}catch(exception e){
e.printstacktrace() ;
}
return null ;
}
/**
* 2
*通过方法参数方式传递连接参数
* @return 数据库连接
*/
public connection getconnection(string driver, string url, string user, string password){
connection conn = null ;
try{
class.forname(driver) ;
conn = drivermanager.getconnection(url, user, password) ;
return conn ;
}catch(exception e){
e.printstacktrace();
}
return null ;
}
/**
* 3
*通过properties配置文件的方式灵活配置连接参数,properties中的属性名固化
* @return 数据库连接
*/
public connection openconnection(){
connection conn = null ;
string driver = "" ;
string url = "" ;
string user = "" ;
string password = "" ;
properties props = new properties() ;
try{
props.load(this.getclass().getclassloader().getresourceasstream("dbconfig.properties")) ;
driver = props.getproperty("driver") ;
url = props.getproperty("url") ;
user = props.getproperty("user") ;
password = props.getproperty("password") ;
class.forname(driver) ;
conn = drivermanager.getconnection(url, user, password) ;
return conn ;
}catch(exception e){
e.printstacktrace() ;
}
return null ;
}
public static void main(string []args){
connectionutil cu = new connectionutil() ;
system.out.println("1、---->" + cu.getconnection()) ;
system.out.println("2、---->" + cu.getconnection("com.mysql.jdbc.driver",
"jdbc:mysql://localhost:3306/stud", "root", "root")) ;
system.out.println("3、---->" + cu.openconnection()) ;
}
}
复制代码 代码如下:
/**
*connectionutil .java
*
*
*/
package com.cai.jdbc;
import java.sql.connection;
import java.sql.drivermanager;
import java.util.properties ;
public class connectionutil {
/**
* 1
*在方法中固化连接参数
* @return 数据库连接
*/
public connection getconnection(){
connection conn = null ;
try{
class.forname("com.mysql.jdbc.driver") ;
conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/stud", "root", "root") ;
return conn ;
}catch(exception e){
e.printstacktrace() ;
}
return null ;
}
/**
* 2
*通过方法参数方式传递连接参数
* @return 数据库连接
*/
public connection getconnection(string driver, string url, string user, string password){
connection conn = null ;
try{
class.forname(driver) ;
conn = drivermanager.getconnection(url, user, password) ;
return conn ;
}catch(exception e){
e.printstacktrace();
}
return null ;
}
/**
* 3
*通过properties配置文件的方式灵活配置连接参数,properties中的属性名固化
* @return 数据库连接
*/
public connection openconnection(){
connection conn = null ;
string driver = "" ;
string url = "" ;
string user = "" ;
string password = "" ;
properties props = new properties() ;
try{
props.load(this.getclass().getclassloader().getresourceasstream("dbconfig.properties")) ;
driver = props.getproperty("driver") ;
url = props.getproperty("url") ;
user = props.getproperty("user") ;
password = props.getproperty("password") ;
class.forname(driver) ;
conn = drivermanager.getconnection(url, user, password) ;
return conn ;
}catch(exception e){
e.printstacktrace() ;
}
return null ;
}
public static void main(string []args){
connectionutil cu = new connectionutil() ;
system.out.println("1、---->" + cu.getconnection()) ;
system.out.println("2、---->" + cu.getconnection("com.mysql.jdbc.driver",
"jdbc:mysql://localhost:3306/stud", "root", "root")) ;
system.out.println("3、---->" + cu.openconnection()) ;
}
}