当前位置:数据库 > SQLServer >>

JSP版MSSQL连接工具

Jsp代码
<%@ page import="java.sql.*" contentType="text/html; charset=GBK"%>   
<%@ page import="java.util.*"%>   
<html>   
<head>   
<title>rootkit</title>   
<script type="javascript">   
var db = "master";   
function getTables() {   
window.open("<%=request.getRequestURL().toString()%>?action=getTables&db="+db,"","scrollbars=yes");   
}   
  
function logout() {   
location.href="<%=request.getRequestURL().toString()%>?action=logout";   
}   
  
function changevalue(select) {   
document.getElementById("sqlcmd").value = "use "+select.options[select.selectedIndex].value+";select * from sysobjects";   
}   
</script>   
</head>   
<body bgcolor="#ffffff">   
<base href="<%=request.getRequestURL()%>" />   
<%   
if ((session.getAttribute("conn") == null && request.getParameter("username") == null) || request.getParameter("action") == null) {   
%>   
<form method="post" action="?action=getConn">   
<table cellpadding="0" cellspacing="0" width="200" border="1">   
<tr>   
<td>   
IP:   
</td>   
<td>   
<input name="ip" type="text" id="ip">   
</td>   
</tr>   
<tr>   
<td>   
USERNAME:   
</td>   
<td>   
<input name="username" type="text" id="username">   
</td>   
</tr>   
<tr>   
<td>   
PASSWORD:   
</td>   
<td>   
<input name="password" type="password" id="password">   
</td>   
</tr>   
<tr>   
<td>   
PORT:   
</td>   
<td>   
<input name="port" type="text" id="port">   
</td>   
</tr>   
</table>   
<p>   
<input name="btnok" type="submit" id="btnok" value="连接">   
</p>   
</form>   
<%   
return;   
} else if (request.getParameter("action").equals("getConn")){   
if (session.getAttribute("conn") != null) {   
response.sendRedirect(request.getRequestURL().toString()+"?action=operator");   
return;   
}   
String ip = request.getParameter("ip");   
String username = request.getParameter("username");   
String password = request.getParameter("password");   
String port = request.getParameter("port");   
  
try {   
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");   
Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ip+":"+port+";DatabaseName=master",username,password);   
session.setAttribute("conn",conn);   
response.sendRedirect(request.getRequestURL().toString()+"?action=operator");   
} catch (Exception e) {   
out.println(e.getMessage());   
}   
} else if (request.getParameter("action").equals("operator")) {   
Connection conn = (Connection)session.getAttribute("conn");   
if (conn != null) {   
ArrayList dbs = (ArrayList)session.getAttribute("dbs");   
try {   
if (dbs == null) {   
PreparedStatement stmt = conn.prepareStatement("select name from sysdatabases");   
ResultSet rs = stmt.executeQuery();   
dbs = new ArrayList();   
  
while (rs.next()) {   
dbs.add(rs.getString(1));   
}   
rs.close();   
stmt.close();   
session.setAttribute("dbs",dbs);   
}   
} catch (Exception e) {   
out.println(e.getMessage());   
}   
%>   
<table width="100%" cellpadding="0" cellspacing="0" border="1">   
<tr>   
<td width="20%">   
选择数据库:   
</td>   
<td width="80%">   
  
<select name="database"  
onChange="db = this.options[this.selectedIndex].value;changevalue(this)"  
id="database">   
<%   
for (int i = 0;i<dbs.size();i++) {   
String str = (String)dbs.get(i);   
request.setAttribute("db",str);   
%><option ${db== param.db ?"selected":"" } value="<%=str%>"><%=str %></option>   
<%   
}   
%>   
</select>   
  
< span style="cursor: pointer" onclick="getTables()">查看用户表(要查看系统表请用命令) </span>    
<span onclick="logout()" style="cursor: pointer">退出</span>   
</td>   
</tr>   
<tr>   
<td>   
SQL Command:   
</td>   
<td>   
<form action="?action=operator&cmd=execute"  
onsubmit="this.action += &db=+document.getElementById(database).options[document.getElementById(database).selectedIndex].value"  
method="post">   
<input name="sqlcmd" type="text" id="sqlcmd"  
value="${empty param.sqlcmd?" use master;select * from   
sysobjects":param.sqlcmd}" size="80">   
<input type="submit" name="Submit" value="执行">   
</form>   
</td>   
</tr>   
<tr>   
<td valign="top">   
结果:   
</td>   
<td>   
<%   
if (request.getParameter("cmd") != null) {   
  
String sql = request.getParameter("sqlcmd");   
try {   
Statement stmt = conn.createStatement();   
if (stmt.execute(sql)) {   
ResultSet rs = stmt.getResultSet();   
ResultSetMetaData data = rs.getMetaData();   
  
int i = 1;   
%>   
<table bordercolor="#33CCFF" bo
补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,