利用Dbutils實現往兩個有關聯的表新增資料

bug不存在的發表於2021-10-08

Register.java(表單傳過來的後臺處理檔案)

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // TODO Auto-generated method stub

        String username=LM.toChinese(request.getParameter("username"));

        String password=request.getParameter("password");

        String sex=LM.toChinese(request.getParameter("sex"));

        System.out.println(sex);

        String email=request.getParameter("email");

        response.setContentType("text/html;charset=utf-8");

        NowTime time=new NowTime();

        String ts=time.getDate();

        String createby=username;

        UserBean user=new UserBean();

        UserImple ui=new UserImple();

        User_Message um=new User_Message();

        

        user=ui.AddUser(username,password,email,sex,createby,ts);

        response.sendRedirect("Login.jsp");

    }

```


UserImple.java(目前是往單一表儲存資料)

public UserBean AddUser(String username, String password, String email, String sex, String createby, String ts) {

        // TODO Auto-generated method stub

        try{

            QueryRunner qr=new QueryRunner(JavaUtils.getPools());

            String sql="insert into user (Username,UserPassword,Email,Sex,Create_Time,Create_By) values (?,?,?,?,?,?);";

            Object[] params={username,password,email,sex,ts,createby};

            qr.update(sql, params);

            return null;

        }catch(SQLException e){

            throw new RuntimeException("註冊成功!");

        }

    }

```


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70007877/viewspace-2794992/,如需轉載,請註明出處,否則將追究法律責任。

相關文章