打开这个K:/ResinServer/conf/resin.conf文件,请相对应地选择你自己的文件,打开编辑(记事本就可以了,最好是VS),如果你的Resin是3.1版本并且配置文件没有更改过的话大约在200行左右有个节点 <database>被隐藏了,如果如果有的话只写修改就行,我的配置是这样的:
<database>
<jndi-name>jdbc/mysql</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/mysql</url>
<user>root</user>
<password>lingxiao</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
然后保存
在你的web项目里面新建一个java类,默认位置(输出目录上面已经设置了),包自己设置
package verys;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
public class DBConn {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private int resultNum = 0;
/**
* 构造函数,作者凌晓 netpetboy@163.com
* 找到数据源,并用这个数据源创建连接
*/
public DBConn()
{
try {
Context env = new InitialContext();
DataSource pool = (DataSource) env.lookup("java:comp/env/jdbc/mysql");
if (pool == null)
throw new Exception("jdbc/mysql is an unknown DataSource");
conn = pool.getConnection();
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println("naming:" + e.getMessage());
}
}
/**
* 执行SQL语句:查询记录
* @param sql SQL语句
* @return ResultSet 记录集
*/
public ResultSet executeQuery(String sql) {
rs = null;
try {
rs = stmt.executeQuery(sql);
} catch(SQLException se) {
System.out.println("Query error:" + se.getMessage());
}
return rs;
}
/**
* 执行SQL语句 :插入与更新记录
* @param sql SQL语句
* @return int resultNum 更新的记录数
*/
public int executeUpdate(String sql) {
resultNum=0;
try {
resultNum = stmt.executeUpdate(sql);
} catch (SQLException se) {
System.err.println("Update error:" + se.getMessage());
}
return resultNum;
}
/**
* 关闭连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException se) {
System.out.println("close error: " + se.getMessage());
}
}
}
修改index。jsp如下
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@page import="verys.DBConn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="verys.DBConn;"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
int count=2;
count=count++;
%>
<br/>
次数:<%=count %>
<%
String select_user_sql="show tables";
String user="root";
String password="iloveyou";
try
{
DBConn con=new DBConn();
if(con==null)
{
%>
为空
<%
}
else
{
try {
ResultSet resultSet =con.executeQuery(select_user_sql);
while(resultSet.next()){
out.print(resultSet.getString(1)+"<br/>");
}
}
catch(SQLException e){
e.printStackTrace();
}
finally {
con.close();
}
}
}
catch(Exception e)
{
%>
发生异常<%e.printStackTrace() ;%>
<%
}
%>
</body>
</html>