sql测试工具
为了提高学习效率,节省点时间,避免一些重复劳动,弄了几个小工具.
主要针对查询数据库表和测试sql语句用的.我用的数据库是mysql.
主要用以下几个文件:
DBConn.java   封装数据库连接与关闭的java文件
DBmysql.java  封装数据库操作的java文件
showtable.jsp 输入表名查询数据库中的这个表的内容
operSql.jsp      针对sql操作的,能够create、insert、update、del
userBean.jsp   调用javabean的一个公用文件
采用的是jsp+javabean模式.
原代码如下:
DBConn.java  文件
package apple;

import java.sql.*;

public class DBConn
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

public DBConn()
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(Exception e)
{
e.printStackTrace();
}
}


public Connection getConnection() throws SQLException
{
Connection conn = null;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/struts","root","123456");
return conn;
}

public boolean closeConn()
{
try{
if(conn!=null) conn.close();
return true; }
catch (SQLException e) {
System.err.println(
"closeConn:"+e.getMessage());
return false; }
}
}
DBmysql.java 文件
这个文件可以再优化一下,其中有几个方法用一个实现。
package apple;

import java.sql.*;

public class DBmysql {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs;
DBConn dbconn = new DBConn();

public DBmysql() {
}

public boolean create(String sql)throws SQLException
    {
       try{
        conn = dbconn.getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
       }
       catch(SQLException ex){
        System.err.println(ex.getMessage());
        return false;
       }
        return true;
    }

public boolean delete(String sql)throws SQLException
    {
       try{
        conn = dbconn.getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
       }
       catch(SQLException ex){
        System.err.println(ex.getMessage());
        return false;
       }
        return true;
    }
    
    public boolean update(String sql)throws SQLException
    {
       try{
        conn = dbconn.getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
       }
       catch(SQLException ex){
        System.err.println(ex.getMessage());
        return false;
       }
        return true;
    }
    
public boolean insert(String sql)throws SQLException
    {
       try{
        conn = dbconn.getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
       }
       catch(SQLException ex){
        System.err.println(ex.getMessage());
        return false;
       }
        return true;
    }
    
    public ResultSet select1(String s)
        throws SQLException
    {
  
        conn = dbconn.getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(s);
        return rs;
    }
    
    public ResultSet select(String sql)throws SQLException
    {
       try{
        conn = dbconn.getConnection();
        stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
       }
       catch(SQLException ex){
        System.err.println(ex.getMessage());
       }
        return rs;
    }
    
    
    public void close(){
     dbconn.closeConn();
    }
    
  
    
}
userBean.jsp文件
<%@page language=
"java" import="java.sql.*" %>
<jsp:useBean id=
"apple" scope="session" class="apple.DBmysql" />
<%!
String sql;
ResultSet rs;  
%>
operSql.jsp 文件    
<html>
<head>
<title>使用Jsp+JAVABEAN访问数据库</title>
</head>
<%@ page contentType=
"text/html;charset=GB2312" %>
<%@ include file=
"useBean.jsp"%>
<body>
<center><h2>
使用Jsp+JAVABEAN访问数据库
</h2></center>
<h3>
欢迎连接MySQL!
</h3>
<%
boolean show=true;
if(request.getParameter(
"go")!=null)
{
  sql=request.getParameter(
"sql");
  String operate=request.getParameter(
"operate");
  out.println(sql+
"<br>");
  out.println(operate+
"<br>");
  if(
"create".equals(operate)&&apple.create(sql)){
  
//apple.create(sql);
     out.println(
"create successful!");
  }
  if(
"insert".equals(operate)&&apple.insert(sql)){
     out.println(
"insert successful!");
  }
   if(
"delete".equals(operate)&&apple.delete(sql)){
     out.println(
"insert successful!");
  }
  if(
"select".equals(operate)){
     rs=apple.select(sql);
while(rs.next()){
     out.println(rs.getString(1));
out.println(rs.getString(2));
out.println(
"<br>");
}
     out.println(
"select successful!");
  }
  apple.close();
}
else{
out.println(
"blank");
}

%>
<center>
<form method=
"post" action="operSql.jsp">
sql:<input type=
"text" name="sql"><p>
create:<input type=
"radio" name="operate" value="create">
insert:<input type=
"radio" name="operate" value="insert">
select:<input type=
"radio" name="operate" value="select">
delete:<input type=
"radio" name="operate" value="delete"><p>
<input type=
"submit" name="go" value="go">
<input type=
"reset" value="reset">
</form>
</center>
showtable.jsp文件
<html>
<
>>
<input type=
"reset" value="reset">
</form>
</center>
showtable.jsp文件
<html>
<head>
<title>使用Jsp+J
="#00bb00">"text/html;charset=GB2312"
%>
<%@ include file=
"useBean.jsp"%>
<body>
<center><h2>
使用JDBC直接访问数据库
</h2></center>
<h3>
欢迎连接MySQL!
</h3>
<%
boolean show=true;
if(request.getParameter(
"goto1")!=null)
{
  String table1=request.getParameter(
"table");
%>
<table border=1 align=
"center">
<tr>
<td>编号</td>
<td>用户名</td>
<td>删除</td>
<td>编辑</td>
</tr>
<%
sql =
"select * from "+table1+"";
out.println(
"info from table:"+table1+"<br>");
rs = apple.select(sql);
while(rs.next())
{
%>
<tr>
<td><%
//=rs.getInt(1)%></td>
<td>
    
      <%=new String(rs.getString(2).getBytes(
"ISO8859_1"),"GBK")%></td>
<td><a href=del.jsp?id=<%
//=rs.getInt(1)%>>del</a></td>
<td><a href=modify.jsp?id=<%
//=rs.getInt(1)%>>modify</a></td>
</tr>
<%
}

apple.close();
show=false;
%>
</table>
<%
}
%>
<%if(show){%>
<center>
<form method=
"post" action="showtable.jsp">
<input type=
"text" name="table">
<input type=
"submit" name="goto1" value="go">
</form>
</center>
<%}%>
<a href =
"operSql.jsp">operSql</a>
</body>
</html>

<a href ="showtable.jsp">show table</a>
</body>
</html>
lunzi   2006-05-23 00:10:25 评论:0   阅读:3008   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog