本文章过于混乱,请移步到另一篇博客 Java JDBC编程
1.mysql 数据库的 jdbc url 形式
"jdbc:mysql://localhost:3036/databasename"注意:lookup 中的 "java:comp/env/"是固定写法,在后面加上资源名称"jdbc/bookstore"
-------------------------------------------------------------------------这是分割线,下面为代码-------------------------------------------------
1.在conf/context.xml中添加资源信息
jdbc/bookstore
javax.sql.DataSource
Container
package zhangweicong.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class GetDBInfoServlet extends HttpServlet {
private String url;
private String user;
private String password;
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection conn= null;
Statement stmt = null;
ResultSet rs=null;
try{
//连接池方式
Context ctx = new InitialContext();
DataSource ds = (DataSource )ctx.lookup("java:comp/env/jdbc/bookstore");
conn = ds.getConnection();
/*
* 建立独立物理连接
conn = DriverManager.getConnection(url,user,password);
*/
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("");
out.println("数据库表信息 ");
out.println("");
String tableName=request.getParameter("tableName");
if( tableName==null||tableName.equals("")){
out.println("点这里");
return;
}else{
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from "+tableName);
ResultSetMetaData rsMeta = rs.getMetaData();
int columnCount = rsMeta.getColumnCount();
out.println("表的结构 ");
out.println("字段名 字段类型 最大字符宽度 ");
ArrayList al =new ArrayList();
for(int i=1;i");
String columnName = rsMeta.getColumnName(i);
out.println(""+columnName+" ");
al.add(columnName);
out.println(""+rsMeta.getColumnTypeName(i)+" ");
out.println(""+rsMeta.getColumnDisplaySize(i)+" ");
}
out.println("
");
}
}catch(SQLException se){
se.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}finally{
if(rs != null){
try{
rs.close();
}catch(SQLException se){
se.printStackTrace();
}
rs=null;
}
if(stmt != null){
try{
stmt.close();
}catch(SQLException se){
se.printStackTrace();
}
stmt=null;
}
if(conn != null){
try{
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
conn=null;
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
//以直接连接数据库方式时,去掉这里的注释
/*ServletContext servletContext = this.getServletContext();
String driverClass = servletContext.getInitParameter("driverClass");
url = servletContext.getInitParameter("url");
user = servletContext.getInitParameter("user");
password = servletContext.getInitParameter("password");
try{
Class.forName(driverClass);
}catch(ClassNotFoundException ce){
throw new UnavailableException("加载数据库驱动失败");
}*/
}
}
GetDBInfoServlet
zhangweicong.web.GetDBInfoServlet
GetDBInfoServlet
/servlet/GetDBInfoServlet
注意:请开启你的服务器和数据库服务器(数据库bookstore要存在bookinfo表,这是对我而言,你的自己看着办吧);