package cn.edu.hpu.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
//定义mysql的数据库连接地址:
public static final String DBDURL = "jdbc:mysql://localhost:3306/iqweb" ;
//mysql数据库的连接用户名
public static final String DBUSER = "root" ;
//mysql数据库的连接密码
public static final String DBPASS = "1234" ;
static{
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn=null;
try {
conn=DriverManager.getConnection(DBDURL,DBUSER,DBPASS);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs,Statement st,Connection conn){
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(Statement st,Connection conn){
close(null,st,conn);
}
}
并且把数据库配置单独放置在porperties配置文件中:
我们在src中创建一个数据库连接类:
package com.cn.opensource.dbConn;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DatabaseConnection {
//初始化上下文需要用到的工厂类
private final static String INITIAL_CONTEXT_FACTORY="weblogic.jndi.WLInitialContextFactory";
//WebLogic服务器的访问地址
private final static String PROVIDER_URL="t3://127.0.0.1:7001";
//WebLogic服务器中的JNDI数据源名称
private final static String MYSQL_JNDI_NAME="JNDI-MySqlDataSource";
//存储从JNDI容器中取出来的数据源
private static DataSource dsOracle = null;
private static DataSource dsMySQL = null;
static {
try {
//初始化WebLogic Server的JNDI上下文信息
Context context = getInitialContext();
//获取数据源对象
dsMySQL = (DataSource) context.lookup(MYSQL_JNDI_NAME);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* MethodName: getInitialContext
* Description: 获得WebLogic ServerJNDI初始上下文信息
* @return
* @throws Exception
*/
private static Context getInitialContext() throws Exception {
Properties properties = new Properties();
//指定工厂类
properties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
//指定服务器访问地址
properties.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(properties);
}
/**
* MethodName: getMySQLConnection
* Description: 获取MySQL数据库连接
* @return
* @throws SQLException
*/
public static Connection getMySQLConnection() throws SQLException {
return dsMySQL.getConnection();
}
/**
* MethodName: CloseConnection
* Description: 关闭数据库连接
* @return
* @throws SQLException
*/
public static void Close()throws SQLException {
if(dsMySQL!=null){
dsMySQL.getConnection().close();
}
}
}
编写Servlet内容:
package com.cn.opensource.dbConn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JndiConnectionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println(" A Servlet ");
out.println(" ");
out.print("测试JNDI数据源连接
");
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = DatabaseConnection.getMySQLConnection();
String sql = "select * from teacher";
st = conn.createStatement();
rs = st.executeQuery(sql);
int i=0;
while (rs.next()) {
i++;
out.println("第"+i+"个教师信息");
out.println("编号:"+rs.getInt("number"));
out.println("姓名:"+rs.getString("name"));
out.println("性别:"+rs.getString("sex"));
out.println("所属班级:"+rs.getString("classname"));
out.println("住址:"+rs.getString("address"));
out.println("
");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
DatabaseConnection.Close();
} catch (SQLException e) {
e.printStackTrace();
}
}
out.println(" ");
out.println("");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
JndiConnectionServlet
com.cn.opensource.dbConn.JndiConnectionServlet
JndiConnectionServlet
/servlet/JndiConnectionServlet
启动服务器,访问应用路径,可以看到:
说明我们配置的数据源连接成功了!
转载请注明出处:http://blog.csdn.net/acmman/article/details/70146603