baidu_27396077 2015-05-11 13:39 采纳率: 10.5%
浏览 1603

导入不进去数据库。求帮忙看看。搞了好久。

package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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 {

public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // 设置中文的输入和输出
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    // 获取输出流对象,并输出信息
    PrintWriter out = response.getWriter();
    // 获取表单提交的数据
    String name = request.getParameter("name");
    double salary = Double.parseDouble(request.getParameter("salary"));
    int age = Integer.parseInt(request.getParameter("age"));
     out.print("添加成功");
    //将数据插入到数据库t_emp表中
    Connection conn = null;
    PreparedStatement stat = null;
    try {

        conn = JdbcUtil.getConnection();

        stat = conn.prepareStatement("insert into user 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{
      JdbcUtil.release(null, stat, conn);
    }
}

}

 create table user(
   name  varchar(20),
   salary varchar(20),
   age int
);

package web;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*

  • 这是jdbc工具类。
    */
    public class JdbcUtil {

    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    private static Properties properties = new Properties();
    //读取配置文件
    static{
    try{
    // 加载配置文件.
    properties.load(JdbcUtil.class.getClassLoader().getResourceAsStream("web/property.properties"));
    driver = properties.getProperty("jdbc.driver");
    url = properties.getProperty("jdbc.url");
    user = properties.getProperty("jdbc.user");
    password = properties.getProperty("jdbc.password");
    Class.forName(driver);
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e);
    }
    }

    public static Connection getConnection() throws Exception {
    Connection conn = DriverManager.getConnection(url,user,password);
    return conn;

    }

    public static void release (ResultSet rs ,Statement stmt,Connection conn){//关闭要返过来关闭
    if(rs!= null){
    try{
    rs.close();
    }catch(SQLException e ){
    e.printStackTrace();
    }
    rs = null;
    }
    if(stmt != null)
    {
    try{
    stmt.close();
    }catch(SQLException e){
    e.printStackTrace();

    }
    stmt = null;

            }
    
    if(conn!=null){
        try{
            conn.close();
        }catch(SQLException e ){
            e.printStackTrace();
        }
        conn = null;
    }
    

    }
    }

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>MyHtml.html</title>

    <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 style = "font-size:24px">
    <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>

上面是servlet类和jdbc工具类和创建的数据表。页面上添加了,但是,存不进去到数据库。求解。

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-05-11 13:41
    关注

    下断点看看,你的服务器方法是否执行,catch 有没有捕获到错误等

    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题