数据库连接类定义:
package mango.ita.dbconnect;
import java.sql.*;
public class amdb {
private static Connection dbconn = null;
private String user;
private String password;
private String url;
private String driver;
public amdb() {
user = "root";
password = "123456";
url = "jdbc:mysql://10.10.6.77:3306/amdb";
driver = "com.mysql.jdbc.Driver";
}
public void getConnection() {
if (dbconn != null) {
return;
}
try {
Class.forName(driver);
dbconn = DriverManager.getConnection(url, user, password);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getDbconn() {
return dbconn;
}
public static void setDbconn(Connection dbconn) {
amdb.dbconn = dbconn;
}
}
直接查询数据库,不通过类:
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contentType="text/html; charset=gb2312"
%>
<html>
<head>
<title>运维平台</title>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>
<body>
<p align="center"><u><font size="5" face="华文新魏">运维平台</font></u></p>
<%!
String host="10.10.6.77"; //数据库主机
String database="amdb"; //数据库名
String user="root"; //用户名
String pass="123456"; //口令
%>
<%
java.sql.Connection sqlConn; //数据库连接对象
java.sql.Statement sqlStmt; //语句对象
java.sql.ResultSet sqlRst; //结果集对象
//登记JDBC驱动对象
Class.forName ("org.gjt.mm.mysql.Driver").newInstance ();
//连接数据库
sqlConn= java.sql.DriverManager.getConnection ("jdbc:mysql://"+host+"/"+database,user,pass);
//创建语句对象
sqlStmt=sqlConn.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//执行Sql语句
String sqlQuery="select distinct pakage,product,'fabu chongqi tongbu' from ita_productor_maintenance order by product asc,pakage asc";
sqlRst=sqlStmt.executeQuery (sqlQuery);
%>
<table border="1" width="100%" bordercolorlight="#CC99FF" cellpadding="2" bordercolordark="#FFFFFF" cellspacing="0">
<tr>
<td align="center">pakage</td>
<td align="center">product</td>
<td align="center">action</td>
</tr>
<% while (sqlRst.next()) { //取得下一条记录 %>
<tr><!--显示记录-->
<td><%=sqlRst.getString("pakage")%></td>
<td><%=sqlRst.getString("product")%></td>
<td><%=sqlRst.getString("fabu chongqi tongbu")%></td>
</tr>
<% }%>
</table>
</body>
<%
//关闭结果集对象
sqlRst.close();
//关闭语句对象
sqlStmt.close ();
//关闭数据库连接
sqlConn.close();
%>
通过类提交数据:
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
contentType="text/html; charset=gb2312"
%>
<html>
<head>
<title>数据维护页面</title>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>
<body>
<form id="vender" action="ptProjectSub.jsp" method="post">
<fieldset>
<legend>基本信息(带*号为必填项)</legend>
<table>
<tr><td align="right"><label for="name">name*:</label></td>
<td><input class="easyui-validatebox" type="text" name="name1" required="true"></input></td>
</tr>
<tr><td align="right"><label for="password">password*:</label></td>
<td> <input class="easyui-validatebox" type="text" name="password1" required="true"></input></td>
</tr>
<tr><td align="right"><label for="usertype">usertype*:</label></td>
<td><input class="easyui-validatebox" type="text" name="usertype1" required="true"></input></td>
<tr><td align="right"><label for="id">id*:</label></td>
<td><input class="easyui-validatebox" type="text" name="id1" required="true"></input></td>
</tr>
</table>
</fieldset>
<span style="display:block; width:500px; text-align:right">
<input type="submit" name="submit" value="保存" />
<input type="reset" name="button" value="修改" />
<input type="button" name="button" value="下一步"/>
</span>
</form>
</body>
</html>
<%@ page language="java"
import="java.util.*"
import="java.sql.*"
import ="java.io.*"
import ="mango.ita.dbconnect.*"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page session="true" %>
<jsp:useBean id="dbConn" class="mango.ita.dbconnect.amdb"/>
<%
request.setCharacterEncoding("utf-8");//处理乱码
String name = request.getParameter("name1");
String password = request.getParameter("password1");
String usertype = request.getParameter("usertype1");
String id = request.getParameter("id1");
String sql1 = "select * from tb_usertable where name='"+name+"'";
amdb amdb = new amdb();
amdb.getConnection();
Connection conn = amdb.getDbconn();
ResultSet rs = conn.createStatement().executeQuery(sql1);
if(rs.next()){
out.println("{\"用户已存在。\"}");
}else{
String sql = "insert into tb_usertable(id,name,password,usertype) values('"+id+"','"+name+"','"+password+"','"+usertype+"')";
boolean bol = conn.createStatement().execute(sql);
if(bol){
out.println("{\"添加成功!\"}");
}else{
out.println("{\"出错了,请重新操作!\"}");
}
}
rs.close(); //释放链接
//conn.close(); //释放链接
response.setHeader("Refresh","30; URL=adddata.jsp");//30秒后自动跳转页面
%>