zoukankan      html  css  js  c++  java
  • 新web系统

    11.26

    今天开始新的web系统的练习,这个web系统真的很普通,只有一个变量,没有遇道什么问题,明天计划继续写web系统 

    代码部分:

    package bean;

    public class TestBean {
    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String name;

    }

    package util;
    import java.sql.*;
    public class DBUtil {
    public static final String connectionURL="jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
    public static final String username="root";
    public static final String psw="123456";
    static Connection connection;
    static ResultSet rSet;
    static PreparedStatement sql;
    public static Connection getConnection()
    {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("数据库连接成功");
    return DriverManager.getConnection(connectionURL, username, psw);
    } catch (Exception e) {
    // TODO: handle exception
    System.out.println("数据库连接失败");
    e.printStackTrace();
    }
    return null;
    }

    public static void close(Statement state, Connection conn) {
    if(state!=null) {
    try {
    state.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(conn!=null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    public static void close(ResultSet rs, Statement state, Connection conn) {
    if(rs!=null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(state!=null) {
    try {
    state.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(conn!=null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    }

    package dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    import bean.TestBean;
    import util.DBUtil;


    public class AddDao {

    public static boolean add(TestBean test) {

    boolean flag=false;
    Connection conn=(Connection)DBUtil.getConnection();

    try {
    String sql="insert into test1205(name)"
    +" values('"+test.getName()+"')";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    int i = pstmt.executeUpdate();
    pstmt.close();
    conn.close();
    if(i>0) {
    flag = true;
    }
    }catch(SQLException e) {
    e.printStackTrace();
    }
    return flag;
    }

    }

    package servlet;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import bean.TestBean;
    import dao.AddDao;


    @WebServlet("/AddServlet")
    public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public AddServlet() {
    super();
    // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String name = request.getParameter("name");
    TestBean test = new TestBean();
    test.setName(name);
    try{
    AddDao.add(test);
    request.setAttribute("message", "注册成功");
    //System.out.println("添加成功!");
    request.getRequestDispatcher("index.html").forward(request,response);
    }catch(Exception e){
    System.out.println("添加失败");
    e.printStackTrace();
    }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

    }

  • 相关阅读:
    String驻留带来的危害
    Go语言的堆栈分析
    SecureCRT使用技巧
    Javascript中相同Function使用多个名称
    记录Office Add-in开发经验
    Silverlight和WPF中DataContractJsonSerializer对时间的处理差异
    ASP.NET MVC项目实践技巧
    有点担心Node.js的未来了
    回首经典的SQL Server 2005
    duilib关于学习Demo中的QQ
  • 原文地址:https://www.cnblogs.com/092e/p/14170108.html
Copyright © 2011-2022 走看看