java连接数据库的几种方法_[整理]Java连接数据库的几种方法

JDBC硬编码连接

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class JDBCConn {

public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

public static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=books";

public static final String USER = "sa";

public static final String PWD = "ok";

private Connection conn = null;

public Connection getConn() {

try {

Class.forName(DRIVER);

conn = DriverManager.getConnection(URL,USER,PWD);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

}

读取propperties配置文件连接

1.创建读取配置文件的类

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

/**

* 单列类:获取属性文件

* @author macau

*/

public class Env extends Properties {

/**

* 构造方法

*/

private Env() {

InputStream is = (InputStream) this.getClass().getResourceAsStream(

"/db.properties");//创建输出流

try {

load(is);//读取流

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

private static Env instance;//当前类实例

/**

* 获得当前实例

* @return

*/

public static Env getInstance() {

if (instance == null) {

makeInstance();

}

return instance;

}

/**

* 创建当前类实例

*/

private static synchronized void makeInstance() {

if (instance == null) {

instance = new Env();

}

}

}

2.通过读取配置文件的类获取数据库连接参数

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class PropConn {

private String driver = Env.getInstance().getProperty("driver");

private String url = Env.getInstance().getProperty("url");

private String user = Env.getInstance().getProperty("user");

private String pwd = Env.getInstance().getProperty("pwd");

private Connection conn = null;

public Connection getConn(){

try {

Class.forName(driver);

conn = DriverManager.getConnection(url,user,pwd);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

}

配置JNDI获取

1.在Tomcat安装目录的conf目录下的context.xml文件的标记中添加配置

maxActive="1000" maxIdel="30" maxWait="10000"

username="sa" password="ok"

driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

url="jdbc:sqlserver://localhost:1433;DatabaseName=books" />

2.编码获取数据库连接

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

public class JNDIConn {

private Connection conn = null;

public Connection getConn() {

try {

Context context = new InitialContext();

DataSource source = (DataSource) context

.lookup("java:comp/env/jdbc/books");

conn = source.getConnection();

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值