Myeclipse 使用JDBC添加员工信息

来源:互联网 发布:淘宝没有扣分的违规 编辑:程序博客网 时间:2024/06/16 06:54

MyEclipse通过JDBC连接MySQL数据库,并使用获取的请求参数值构建insert语句,执行后完成员工信息的增加操作。

步骤:

Step1:下载MySQL的JDBC驱动,并将驱动加载到项目中。

个人使用的是:mysql-connector-java-5.1.13.zip,所需要的就是解压缩之后其中的 mysql-connector-java-5.1.13-bin.jar
网址:MySQL的JDBC驱动

Step2:: 设计数据库表

我采用的Navicat for MySQL工具,设计的表如下:


2、新建AddEmpServlet.java类,连接数据库

代码如下:

package web;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AddEmpServlet extends HttpServlet{protected void service(HttpServletRequest request,            HttpServletResponse response)            throws ServletException,IOException{//保证正确读取Post提交来的中文request.setCharacterEncoding("utf-8");//保证正确输出中文response.setContentType("text/html;charset=utf-8");//获取输出流对象,并输出信息PrintWriter out=response.getWriter();//获取表单提交的数据String name= request.getParameter("name");double salary = Double.valueOf(request.getParameter("salary"));int age = Integer.valueOf(request.getParameter("age"));//将数据插入到数据库t_emp表中Connection conn = null;java.sql.PreparedStatement stat=null;try {//1、加载MySQL的JDBC的驱动Class.forName("com.mysql.jdbc.Driver");//2、取得连接的URL,能访问MySQL数据库的用户名和密码String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";String username = "root";String password = "123456"; //3、创建与MySQL数据库的连接类的实例conn=DriverManager.getConnection(url,username,password);stat=conn.prepareStatement("insert into t_emp(name,salary,age) values (?,?,?)");stat.setString(1, name);stat.setDouble(2, salary);stat.setInt(3, age);stat.executeUpdate();out.println("添加成功");} catch (Exception e) {e.printStackTrace();out.print("系统繁忙,稍后重试");}finally{if(stat!=null){try {stat.close();} catch (SQLException e) {e.printStackTrace();}}}}}

step3:修改web.xml文件

如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <servlet>  <servlet-name>addEmpServlet</servlet-name>  <servlet-class>web.AddEmpServlet</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name>addEmpServlet</servlet-name>  <url-pattern>/add</url-pattern>  <!-- 之前必须是addEmp.jsp,提交表单后出现在地址栏的地址 -->  </servlet-mapping></web-app>

step4:添加员工信息界面设计

如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>Servlet program</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><meta http-equiv="content-type" content="text/html;charset=utf-8"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <form action="add" method="post">   <fieldset>  <legend>添加员工</legend>  姓名:<input name="name"/><br>  薪水:<input name="salary"/><br>  年龄:<input name="age"/><br>  <input type="submit" value="添加">  </fieldset>  </form>  </body></html>


step5:部署应用,访问addEmp.jsp,查看结果


step6:访问数据库,检查是否插入成功

其中出现了乱码问题,尝试了很多方法、还未解决。



0 0