第十七天mvc設計模式、事務、Servlet

MVC設計模式(model、view、control)

package com.java.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLOperate {
    //單例設計模式
    private static SQLOperate operate;//靜態對象
    private SQLOperate(){//私有構造器
    }
    private static SQLOperate newInstance(){//靜態得到對象的方法,加入條件語句
        if(operate==null){
            operate=new SQLOperate();
        }
        return operate;
    }
    /**
     * 
     * @param userName 傳入用戶名
     * @param password 傳入密碼
     * @return 如果結果爲true,登錄成功,結果爲false,登錄失敗
     */
    public static boolean login(String userName,String password ){
        Connection con=SQLManager.newInstance().getConnection();
        try {

            if(!con.isClosed()){
                PreparedStatement prepare=con.prepareStatement("select *from user where user_name=?and password=?");
                prepare.setString(1, userName);
                prepare.setString(2,password);
                ResultSet set=prepare.executeQuery();
                set.last();
                int num=set.getRow();
                System.out.println(num);
                return num==1;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        return false;
    }
}
package com.java.sql;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.event.ActionEvent;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(112, 29, 66, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(112, 71, 66, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel = new JLabel("用戶名");
        lblNewLabel.setBounds(48, 32, 54, 15);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("密碼");
        lblNewLabel_1.setBounds(48, 74, 54, 15);
        contentPane.add(lblNewLabel_1);

        JButton btnNewButton = new JButton("登錄");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String userName=textField.getText();
                Pattern p=Pattern.compile("\\p{Alnum}{1,}");
                Matcher matcher=p.matcher(userName);
                Boolean b=true;
                b=matcher.matches();
                if(b){
                    System.out.println("用戶名格式正確");
                }
                String password=textField_1.getText();
                Boolean isLogin=SQLOperate.login(userName, password);
                if(isLogin){
                    System.out.println("登錄成功");
                }else{
                    System.out.println("登錄失敗");
                }
            }
        });
        btnNewButton.setBounds(103, 116, 93, 23);
        contentPane.add(btnNewButton);
    }
}
省略SQLManager和註冊代碼,同第十六天

事務

String insert1="insert into student(id,name,sex,age)value(2,'李四',0,22)";    
String insert2="insert into student(id,names,sex,age)value(3,'小明',1,21)";   //一條語句出現錯誤,其他都不執行   
String insert3="insert into student(id,name,sex,age)value(4,'王五',0,25)";
con.setAutoCommit(false);
statement.addBatch(insert1);
statement.addBatch(insert2);
statement.addBatch(insert3);
statement.executeBatch();
con.commit();

tomcat、Servlet

doGet在網址後面跟上要提交的數據
這裏寫圖片描述

使用瀏覽器提交數據默認的編碼格式爲ISO-8859-1

得到string username的編碼格式爲8859-1
使用ISO-8859-1編碼格式轉化成字節數組
使用UTF-8編碼格式將字節數組轉化成字符串

package com.java.test;
import java.io.UnsupportedEncodingException;
public class Encoding {
    public static String doEncoing(String string) {
        try {
            byte[] array=string.getBytes("ISO-8859-1");
            string=new String(array,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string;
    }
}

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName=request.getParameter("username");
        String password=request.getParameter("password");
        userName=Encoding.doEncoing(userName);
        System.out.println(""+userName+","+password+"");


        String s="得到的用戶名爲:"+userName+" 密碼爲:"+password;
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.getWriter().append(s);

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章