servlet连接mysql数据库
1.tomcat6编译servlet要用到servlet.api.jar包,所以须将其路径加入到classpath之中
2.servlet程序:
package com.ser;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.db.conn//导入连接数据库的javabean程序,在servlet中实例化使用
public class Hserv extends HttpServlet {
private static final long serialVersionUID = -948978577054159141L;
private String username;
private String pass;
private String tel;
private String age;
conn cndb=new conn();
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
username = request.getParameter("username");
pass=request.getParameter("pass");
tel=request.getParameter("tel");
age=request.getParameter("age");
response.setContentType("text/html;charset=GB2312");//这句必须放在PrintWriter
//out=response.getWriter();前面,不然输出中文依然为乱码。
PrintWriter out=response.getWriter();
request. setCharacterEncoding("gb2312");
String sql="insert into comser(username,pass,tel,age) values ('"+username+"','"+pass+"','"+tel+"','"+age+"')";
try {
try {
cndb.connDB();//连接数据库
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cndb.UpdateSQL(sql);//执行插入语句
out.print("<html><body>");
out.print("插入成功!!");
out.print("</body></html>");
cndb.closeDB();//关闭数据库
} catch (SQLException e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
super.init();
}
}
web.xml文件中添加如下配置:
<display-name>hserv</display-name>
<servlet-name>Hserv</servlet-name>//servlet名称
<servlet-class>com.ser.Hserv</servlet-class>//servlet指向的类文件
</servlet>
<servlet-mapping>
<servlet-name>Hserv</servlet-name>
<url-pattern>/Hserv</url-pattern>//web访问时servlet的相对路径
javabean程序--com.db.conn
package com.db;
import java.sql.*;
public class conn {
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
public void connDB()throws Exception{
String url="jdbc:mysql://localhost:3306/login";//login--数据库名
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//下载mysql驱动mysql-connector-java-3.1.14-bin.jar 存放于项目的web-inf/lib目录及tomcat的lib目录下
con=DriverManager.getConnection(url, "root", "1702313");
}
public void closeDB()throws SQLException{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(con!=null){
con.close();
con=null;
}
}
public ResultSet SelectedSql(String sql)throws SQLException{
if(sql==null||sql.equals(" "))
return null;
sql=sql.trim();//去掉字符串两边的空格
stmt=con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
return rs;
}
public void UpdateSQL(String sql)throws SQLException{
stmt=con.createStatement();
stmt.executeUpdate(sql);
}
}
前台jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<center>
<form action="Hserv" method="post" >
<h1> 用户注册程序</h1>
<table border="1" width="400">
<tr><td>
用户名:</td>
<td><input name="username" type="text"/></td>
</tr>
<tr><td width="100">
密 码:</td>
<td><input name="pass" type="password" /> </td></tr>
<tr><td width="100">
电 话:</td>
<td><input name="tel" type="text" /> </td></tr>
<tr><td width="100">
年 龄:</td>
<td><input name="age" type="text" /> </td></tr>
<tr><td><input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
用javac分别编译javabean和servlet的java文件,将生成的class文件放到web-inf/classes文件夹
下,如果有打包,则要安包路径存放。如果用MyEclipse编译就不需要自己手动进行了。