1. 环境的搭建
软件
数据库 sql
myeclipse 8.0 tomcat 6.0
2. 安装完 myeclipse 配置下 部署tomcat 6.0
=1=》
=2=》 新建 jsp 工程
=3=》 新建 jsp 文件信息
启动服务并且部署
代码详情:
建立数据库
1.sql 数据库
CREATE TABLE [dbo].[test1]( [id] [int] IDENTITY(1,1) NOT NULL, [tname] [char](10) NOT NULL, [tband] [char](10) NOT NULL, [tchangdi] [char](20) NOT NULL, [tchangjia] [char](50) NOT NULL, [beizhu] [char](50) NOT NULL ) ON [PRIMARY]
2. 用户各种字段 一般上和我们数据库字段相同
po/User.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package po; 2 public class User { 3 //ctrl+shift+s 再按+c +o +r 自动生成 4 //为了防止直接使用源数据 进行的误操作 5 int id; 6 String tname; 7 String tband; 8 String tchangdi; 9 String tchangjia; 10 String beizhu; 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getTname() { 18 return tname; 19 } 20 public void setTname(String tname) { 21 this.tname = tname; 22 } 23 public String getTband() { 24 return tband; 25 } 26 public void setTband(String tband) { 27 this.tband = tband; 28 } 29 public String getTchangdi() { 30 return tchangdi; 31 } 32 public void setTchangdi(String tchangdi) { 33 this.tchangdi = tchangdi; 34 } 35 public String getTchangjia() { 36 return tchangjia; 37 } 38 public void setTchangjia(String tchangjia) { 39 this.tchangjia = tchangjia; 40 } 41 public String getBeizhu() { 42 return beizhu; 43 } 44 public void setBeizhu(String beizhu) { 45 this.beizhu = beizhu; 46 } 47 public User(int id, String tname, String tband, String tchangdi, 48 String tchangjia, String beizhu) { 49 super(); 50 this.id = id; 51 this.tname = tname; 52 this.tband = tband; 53 this.tchangdi = tchangdi; 54 this.tchangjia = tchangjia; 55 this.beizhu = beizhu; 56 } 57 public User() { 58 super(); 59 // TODO Auto-generated constructor stub 60 } 61 }
3.由于每次使用都会需要调用连接数据库代码 ,所以把抽离出来
db/DbConn.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package db; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class DbConn { 10 11 public static Connection getConn() 12 { 13 Connection con =null; 14 try { 15 // Class.forName("com.mysql.jdbc.Driver"); // 加载驱动程序 16 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 17 18 } catch (ClassNotFoundException e) { 19 System.out.println("加载驱动程序错误" + e.getMessage()); 20 } 21 22 try { 23 // 创建连接 testdb是数据库名称 重点在此 24 con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=mvce", "sa", "123456"); 25 26 } catch (SQLException e) { 27 28 System.out.println("数据库连接操作出错" + e.getMessage()); 29 } 30 return con; 31 } 32 }
4.数据处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import db.DbConn; 12 import po.User; 13 14 public class UserDAO { 15 16 //增加 17 18 public int add(User u) 19 { 20 int n=0; 21 try { 22 // 创建连接 testdb是数据库名称 23 Connection con = DbConn.getConn(); 24 25 // 创建声明SQL对象 26 Statement stm = con.createStatement(); 27 // 执行SQL语句,得到结果集,结果集放到ResultSet对象中 28 String sql="insert into test1 values('"+u.getTname()+"','"+u.getTband()+"','"+u.getTchangdi()+"','"+u.getTchangjia()+"','"+u.getBeizhu()+"');"; 29 n=stm.executeUpdate(sql); 30 } catch (SQLException e) { 31 32 System.out.println("数据库操作出错" + e.getMessage()); 33 } 34 return n; 35 } 36 }
5.设计add.jsp (按照执行顺序)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <p>商品信息添加</p> 11 <form name="frm1" action="servlet/AddServlet"> 12 <p>商品编号: 13 <label for="textfield"></label> 14 <input type="text" name="id" id="textfield" /> 15 </p> 16 <p>商品名字: 17 <input type="text" name="tname" id="textfield2" /> 18 </p> 19 <p>商品品牌: 20 <input type="text" name="tband" id="textfield3" /> 21 </p> 22 <p>商品产地: 23 <input type="text" name="tchangdi" id="textfield4" /> 24 </p> 25 <p>生产厂家: 26 <input type="text" name="tchangjia" id="textfield5" /> 27 </p> 28 <p>备注: 29 <input type="text" name="tbeizhu" id="textfield5" /> 30 </p> 31 32 <p> </p> 33 <input type="submit" value="确定"/> 34 </form> 35 </body> 36 </html>
6.AddServlet.jsp 这个文件一定要新建 ,不要拽,因为web.xml文件不会自动生成(严格来说新建的好)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.sql.*; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import po.User; 13 14 import dao.UserDAO; 15 import db.DbConn; 16 17 public class AddServlet extends HttpServlet { 18 19 /** 20 * Constructor of the object. 21 */ 22 public AddServlet() { 23 super(); 24 } 25 26 /** 27 * Destruction of the servlet. <br> 28 */ 29 public void destroy() { 30 super.destroy(); // Just puts "destroy" string in log 31 // Put your code here 32 } 33 34 /** 35 * The doGet method of the servlet. <br> 36 * 37 * This method is called when a form has its tag value method equals to get. 38 * 39 * @param request the request send by the client to the server 40 * @param response the response send by the server to the client 41 * @throws ServletException if an error occurred 42 * @throws IOException if an error occurred 43 */ 44 public void doGet(HttpServletRequest request, HttpServletResponse response) 45 throws ServletException, IOException { 46 //这样可以调用 可以和 提交方法无关 无论哪种提交都会处理一样的信息 47 doPost(request,response); 48 } 49 public void doPost(HttpServletRequest request, HttpServletResponse response) 50 throws ServletException, IOException { 51 52 response.setContentType("text/html"); 53 PrintWriter out = response.getWriter(); 54 out 55 .println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); 56 out.println("<HTML>"); 57 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 58 out.println(" <BODY>"); 59 // 获取提交的信息 60 String id=request.getParameter("id"); 61 String tname=request.getParameter("tname"); 62 String tband=request.getParameter("tband"); 63 String tchangdi=request.getParameter("tchangdi"); 64 String tchangjia=request.getParameter("tchangjia"); 65 String tbeizhu=request.getParameter("tbeizhu"); 66 67 //把获取的放进对象里面 68 // user 是各种数据字段 69 User u=new User(); 70 u.setTname(tname); 71 u.setTband(tband); 72 u.setTchangdi(tchangdi); 73 u.setTchangjia(tchangjia); 74 u.setBeizhu(tbeizhu); 75 //新建数据处理对象 是为了调用这个UserDao 里面的 add 方法使用的 或其他方法使用的(增删改查登录等) 和数据库进行交互的的 76 UserDAO udao=new UserDAO(); 77 //调用方法 成功返回1 不成功返回0 78 int n=udao.add(u); 79 if(n>0) { 80 out.println("tian success"); 81 } 82 else 83 { 84 out.println("添加失败"); 85 } 86 out.println(" </BODY>"); 87 out.println("</HTML>"); 88 out.flush(); 89 out.close(); 90 } 91 public void init() throws ServletException { 92 // Put your code here 93 } 94 95 }
注意:由于myeclipse 软件, 我在 执行中遇到打不来add.jsp
原因:1.重新配置需要 需要重启下tomcat
2.http:// 这个要加上 http://127.0.0.1:8080/js185567/add.jsp