在JSP中举例说明使用JDBC分别实现对数据库的增删改查的步骤?
展开全部
建议先找本JSP页面开发的书本看, 直接给你例子帮助不是很大,因为开发涉及的东西很多:
下面是一个很简单的例子,实际开发不会这么做:
addNews.jsp页面:
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>添加新闻</title>
</head>
<body>
<h3>新闻添加</h3>
<hr/>
<form action="insertData.jsp" method="post">
<table align="center" cellpadding="0" cellspacing="0" width="800px">
<tr>
<td>标题:<input type="text" name="title" size="50"/></td>
</tr>
<tr>
<td>内容:<textarea name="content" rows="10" cols="50"></textarea>
</tr>
<tr>
<td>作者:<input type="text" name="author" size="20" /></td>
</tr>
<tr>
<td>发布时间:<input type="text" name="post_time" size="20" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
</tr>
</table>
<a href="newsList.jsp">返回</a>
</form>
</body>
</html>
insertdata.jsp页面:
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String title = new String(request.getParameter("title").getBytes("iso_8859_1"),"utf-8");
String content = new String(request.getParameter("content").getBytes("iso_8859_1"),"utf-8");
String author = new String(request.getParameter("author").getBytes("iso_8859_1"),"utf-8");
String post_time = new String(request.getParameter("post_time").getBytes("iso_8859_1"),"utf-8");
String msg = "";
Connection conn = null;
Statement stmt = null;
String sql = "insert into news(title,content,author,post_time) values('"+title+"','"+content+"','"+author+"','"+post_time+"')";
try{
String url = "jdbc:mysql://127.0.0.1:3306/task";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "123456");
stmt = conn.createStatement();
stmt.executeUpdate(sql);
msg = "数据录入成功!";
}
catch(Exception e){
msg = "数据录入发生异常!";
e.printStackTrace();
}
finally{
conn.close();
}
out.println(msg);
%>
<a href="newsList.jsp">返回</a>
下面是一个很简单的例子,实际开发不会这么做:
addNews.jsp页面:
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>添加新闻</title>
</head>
<body>
<h3>新闻添加</h3>
<hr/>
<form action="insertData.jsp" method="post">
<table align="center" cellpadding="0" cellspacing="0" width="800px">
<tr>
<td>标题:<input type="text" name="title" size="50"/></td>
</tr>
<tr>
<td>内容:<textarea name="content" rows="10" cols="50"></textarea>
</tr>
<tr>
<td>作者:<input type="text" name="author" size="20" /></td>
</tr>
<tr>
<td>发布时间:<input type="text" name="post_time" size="20" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
</tr>
</table>
<a href="newsList.jsp">返回</a>
</form>
</body>
</html>
insertdata.jsp页面:
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String title = new String(request.getParameter("title").getBytes("iso_8859_1"),"utf-8");
String content = new String(request.getParameter("content").getBytes("iso_8859_1"),"utf-8");
String author = new String(request.getParameter("author").getBytes("iso_8859_1"),"utf-8");
String post_time = new String(request.getParameter("post_time").getBytes("iso_8859_1"),"utf-8");
String msg = "";
Connection conn = null;
Statement stmt = null;
String sql = "insert into news(title,content,author,post_time) values('"+title+"','"+content+"','"+author+"','"+post_time+"')";
try{
String url = "jdbc:mysql://127.0.0.1:3306/task";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "123456");
stmt = conn.createStatement();
stmt.executeUpdate(sql);
msg = "数据录入成功!";
}
catch(Exception e){
msg = "数据录入发生异常!";
e.printStackTrace();
}
finally{
conn.close();
}
out.println(msg);
%>
<a href="newsList.jsp">返回</a>
展开全部
tcp/ip
没有启用吧
在sql
server
2005中有个配置工具选择sql
server
configuration
manager
有个sql
server
2005
网络配置
选中mssqlserver双击
有个tcp/ip
打开
把协议中已启用改为是
ip地址中ip动态端口已启用改为是
没有启用吧
在sql
server
2005中有个配置工具选择sql
server
configuration
manager
有个sql
server
2005
网络配置
选中mssqlserver双击
有个tcp/ip
打开
把协议中已启用改为是
ip地址中ip动态端口已启用改为是
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在JSP里面最好通过类来实现,直接在jsp里调用数据库可能会导致代码很乱而且不安全。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询