<%
/**
2011-01-11
oracle数据库简单查询
*/
%>
<%@page import="java.sql.*"%>
<%
//1.变量定义
//提交的sql
String strCmd = request.getParameter("cmd");
//输出的流
StringBuffer sbCmd = new StringBuffer("");
//提交的action
String strAction = request.getParameter("action");
%>
<%!//2.数据库连接
public class DBconn {
String DBDriver = "oracle.jdbc.driver.OracleDriver";
String ConnStr = "jdbc:oracle:thin:@192.168.0.1:1521:databasename";
String MyUser = "username";
String MyPassword = "password";
Connection conn = null;
ResultSet rs = null;
public DBconn() {
try {
Class.forName(DBDriver);
} catch (java.lang.ClassNotFoundException e) {
System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(ConnStr, MyUser, MyPassword);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println("aq.executeQuery:" + ex.getMessage());
}
return rs;
}
}
%>
<%
//3.方法处理
if (strAction!=null && strAction.equals("mysql")) {
DBconn mysqldb = new DBconn();
ResultSet rs = mysqldb.executeQuery(strCmd);
int len=rs.getMetaData().getColumnCount();
String[] v_fieldname = new String[len]; //表字段名称
for(int t =0 ;t<len;t++) {
String name=rs.getMetaData().getColumnName(t+1);
v_fieldname[t]=name;
sbCmd.append(name+"/t");
}
sbCmd.append("/r/n");
while (rs.next()) {
for(String fieldname:v_fieldname){
String value="";
Object mm=rs.getObject(fieldname);
if(mm!=null){
value=mm.toString();
}
sbCmd.append(value+"/t");
}
sbCmd.append("/r/n");
}
rs.close();
}
%>
<div id="menu2" class="tabcontent">
<form name="cmd" action="" method="post">
<input type="hidden" name="action" value="mysql">
<input type="text" id="cmdvalue" name="cmd" value="<%=strCmd%>"
size=50>
<input type=submit name=submit value="提交">
</form>
<%
if (sbCmd != null && sbCmd.toString().trim().equals("") == false) {
%>
<TEXTAREA NAME="cqq" ROWS="100%" COLS="100%"><%=sbCmd.toString()%></TEXTAREA>
<br>
<%
}
%>
</DIV>
<br>
<br>