jsp连接mysql需要引入一个jar包,下载地址:http://dev.mysql.com/downloads/connector/j/
解压后把mysql-connector-java-5.1.39-bin.jar导入到eclipse中
然后在拷贝一份到tomcat下的bin目录下
然后就可以测试增删改查了
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr><th>id</th><th>用户名</th><th>密码</th></tr>
<%
Class.forName("com.mysql.jdbc.Driver");
String host = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8" ;
String user = "root";
String pwd="";
Connection conn = DriverManager.getConnection(host, user, pwd);
Statement stmt = conn.createStatement();
int res = stmt.executeUpdate("INSERT INTO users(username,password)values('zhangsan','123456')");
out.println(res);
res = stmt.executeUpdate("UPDATE users set username='张三' where username='zhangsan'");
out.println(res);
res = stmt.executeUpdate("DELETE FROM users WHERE id=1");
out.println(res);
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while(rs.next()){
out.println("<tr><td>"+rs.getString("id")+"</td><td>"+rs.getString("username")+"</td><td>"+rs.getString("password")+"</td></tr>");
}
conn.close();
%>
</table>
</body>
</html>