数据库为SQL SERVER 2000
<%--
Document : execsql
Created on : 2009-7-17, 23:26:42
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="GB2312"%>
<%@page import="java.sql.*" %>
<%!
//得到一个数据库的连接
Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=contacts";
String user = "sa";// 这里替换成你自已的数据库用户名
String password = "";// 这里替换成你自已的数据库用户密码
return DriverManager.getConnection(url, user, password);
}
//判断一个字符串是否为空
boolean isEmptyString(String str) {
return str == null || str.length() == 0 || str.trim().length() == 0;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>WEB数据库维护系统</title>
<style>
TD,INPUT,SELECT {font-size:12px}
</style>
<script language="javascript">
function checkvalue()
{
}
</script>
</head>
<body>
<center>
WEB数据库维护系统
<p>
<!--显示输入框,供用户输入需要执行的SQL语句-->
<table width="80%" border="0">
<tr><td>请输入您要执行的SQL语句:</td></tr>
<tr>
<form name="main" action="execsql.jsp" method="POST" onsubmit="return checkvalue();">
<td>
<textarea name="sql" rows="15" cols="80"></textarea>
</td>
<td valign="bottom"><input type="submit" value="执行" /></td>
</form>
</tr>
</table>
<hr>
<%--接收用户的输入,显示查询的结果--%>
<%
//获得用户输入的SQL语句
String sql = request.getParameter("sql");
if (!isEmptyString(sql)) {
out.println("您所执行的sql语句为:" + sql + "<br>");
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = getConnection();
st = conn.createStatement();
if (st.execute(sql)) {
//执行结果为
rs = st.getResultSet();
//得到ResultSet的描述信息
ResultSetMetaData rsmd = rs.getMetaData();
//得到ResultSet的列数目
int nColumnCount = rsmd.getColumnCount();
out.println("您的查询结果为:<br>");
out.println("<table width=\"90%\" border=\"1\">");
out.println("<tr>");
for (int i = 1; i <= nColumnCount; i++) {
out.println("<td><b>" + rsmd.getColumnName(i) + "</b></td>");
}
out.println("</tr>");
while (rs.next()) {
out.println("<tr>");
for (int i = 1; i <= nColumnCount; i++) {
out.println("<td>" + rs.getString(i) + "</td>");
}
out.println("</tr>");
}
} else {
//执行结果为正数
out.println("更新记录的数目为:" + st.getUpdateCount());
}
} catch (SQLException ex) {
out.println(ex);
} finally {
//关闭,释放资源
try {
if (rs != null) {
rs.close();
}
} catch (Exception ex) {
}
try {
if (st != null) {
st.close();
}
} catch (Exception ex) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
}
}
%>
</p>
</center>
</body>
</html>
Document : execsql
Created on : 2009-7-17, 23:26:42
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="GB2312"%>
<%@page import="java.sql.*" %>
<%!
//得到一个数据库的连接
Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=contacts";
String user = "sa";// 这里替换成你自已的数据库用户名
String password = "";// 这里替换成你自已的数据库用户密码
return DriverManager.getConnection(url, user, password);
}
//判断一个字符串是否为空
boolean isEmptyString(String str) {
return str == null || str.length() == 0 || str.trim().length() == 0;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>WEB数据库维护系统</title>
<style>
TD,INPUT,SELECT {font-size:12px}
</style>
<script language="javascript">
function checkvalue()
{
}
</script>
</head>
<body>
<center>
WEB数据库维护系统
<p>
<!--显示输入框,供用户输入需要执行的SQL语句-->
<table width="80%" border="0">
<tr><td>请输入您要执行的SQL语句:</td></tr>
<tr>
<form name="main" action="execsql.jsp" method="POST" onsubmit="return checkvalue();">
<td>
<textarea name="sql" rows="15" cols="80"></textarea>
</td>
<td valign="bottom"><input type="submit" value="执行" /></td>
</form>
</tr>
</table>
<hr>
<%--接收用户的输入,显示查询的结果--%>
<%
//获得用户输入的SQL语句
String sql = request.getParameter("sql");
if (!isEmptyString(sql)) {
out.println("您所执行的sql语句为:" + sql + "<br>");
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = getConnection();
st = conn.createStatement();
if (st.execute(sql)) {
//执行结果为
rs = st.getResultSet();
//得到ResultSet的描述信息
ResultSetMetaData rsmd = rs.getMetaData();
//得到ResultSet的列数目
int nColumnCount = rsmd.getColumnCount();
out.println("您的查询结果为:<br>");
out.println("<table width=\"90%\" border=\"1\">");
out.println("<tr>");
for (int i = 1; i <= nColumnCount; i++) {
out.println("<td><b>" + rsmd.getColumnName(i) + "</b></td>");
}
out.println("</tr>");
while (rs.next()) {
out.println("<tr>");
for (int i = 1; i <= nColumnCount; i++) {
out.println("<td>" + rs.getString(i) + "</td>");
}
out.println("</tr>");
}
} else {
//执行结果为正数
out.println("更新记录的数目为:" + st.getUpdateCount());
}
} catch (SQLException ex) {
out.println(ex);
} finally {
//关闭,释放资源
try {
if (rs != null) {
rs.close();
}
} catch (Exception ex) {
}
try {
if (st != null) {
st.close();
}
} catch (Exception ex) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
}
}
%>
</p>
</center>
</body>
</html>