MVC设计模式,预处理,转码(2015.8.11)

这里写图片描述

数据库模糊查询

select *from user where userName like'张%'
//%可以代替0或多个字符;-下划线可以代替一个字符;

预处理

public boolean Login(String userName, String password) {
        Connection conn = SQLManager.newInstance().getConn();
        // 通过Connection的PrepareStatemen()方法,对SQL语句进行预处理
        try {
            if (!conn.isClosed()) {
                state = (PreparedStatement) conn.prepareStatement("select *from user where userName=?and password=?");
                state.setString(1, userName);
                state.setString(2, password);
                ResultSet set = state.executeQuery();
                set.last();
                int num = set.getRow();
                System.out.println(num);
                return true;

            }

        } catch (SQLException e1) {

            e1.printStackTrace();

        }return false;

request.getParameter

System.out.println(request.getParameter("username"));
http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan

关于编码错乱的处理

public class Encoding {

    public static String doEncoding(String string) {

        try {
            byte[] array = string.getBytes("ISO-8859-1");
            string = new String(array, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return string;

    }
}
************************************************************
String userName = request.getParameter("username");
        String password = request.getParameter("password");

        userName = Encoding.doEncoding(userName);
        System.out.println(userName);
        response.setHeader("content-type", "text/HTML;charset=UTF-8");
        // 让浏览器以utf-8的格式解析
        response.getWriter().append("服务器收到用户名和密码 " + userName + password);

MVC设计模式

model,view,control分开

SQLManager负责连接数据库
package com.sql.java;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 
 * @author单例设计模式
 * 1私有构造器
 * 2静态公开的方法
 * 3私有静态的属性
 * 4方法加线程锁
 *
 */

public class SQLManager {
    private Statement statement;//第一种方法使用
    private Connection conn;


    public Connection getConn() {
        return conn;
    }


    public void setConn(Connection conn) {
        this.conn = conn;
    }


    public Statement getStatement() {
        return statement;
    }


    public void setStatement(Statement statement) {
        this.statement = statement;
    }


    //私有静态的属性
    private static SQLManager manager;
    //静态公开的方法
    public static synchronized SQLManager newInstance() {
        if (manager == null) {
            manager = new SQLManager();
        }
        return manager;
    }


    //私有构造器
    private SQLManager() {

        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/clazz";
        String user = "root";
        String password = "123456";

        try {
            Class.forName(driver);// 加载驱动
            // 与数据库建立连接
            conn = DriverManager.getConnection(url, user, password);


        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
SQLOperate负责控制
package com.sql.java;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.PreparedStatement;

public class SQLOperate {

    private static SQLOperate operate;
    private PreparedStatement state;

    public static synchronized SQLOperate newInstance() {
        if (operate == null) {
            operate = new SQLOperate();
        }
        return operate;
    }
/**
 * 
 *  登录方法
 * 
 */
    public boolean Login(String userName, String password) {
        Connection conn = SQLManager.newInstance().getConn();
        // 通过Connection的PrepareStatemen()方法,对SQL语句进行预处理
        try {
            if (!conn.isClosed()) {
                state = (PreparedStatement) conn.prepareStatement("select *from user where userName=?and password=?");
                state.setString(1, userName);
                state.setString(2, password);
                ResultSet set = state.executeQuery();
                set.last();
                int num = set.getRow();
                System.out.println(num);
                return true;

            }

        } catch (SQLException e1) {

            e1.printStackTrace();

        }return false;

    }

    /**
     * 
     * 注册方法
     * 
     */


    public  boolean Register(String userName, String password) {

        Statement state = SQLManager.newInstance().getStatement();
        // 查询
        String sql = "select * from user where userName='" + userName + "'";
        try {
            ResultSet set = state.executeQuery(sql);
            set.last();// 游标移动到最后一行
            int num = set.getRow();
            System.out.println(num);
            if (num > 0) {
                System.out.println("该用户已存在");
            } else {// 查询没有然后注册

                String register = "insert into user(userName,password)values('" + userName + "','" + password + "') ";
                state.executeQuery(register);
                System.out.println("注册成功");
            }
            return true;

        } catch (SQLException e) {

            e.printStackTrace();
        }
        return false;

    }
}

界面LoginView和RegisterView负责

btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String userName=textFieldUserName.getText();
                String password=textFieldpassword.getText();

                boolean isLogin=SQLOperate.newInstance().Login(userName, password);//类名调用static方法
                if(isLogin){
                    System.out.println("登陆成功");
                }else{
                    System.out.println("登录失败");
                }

            }
        });
***********************************************
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String userName=textFieldUserName.getText();
                String password=textFieldPassword.getText();
                boolean isRegister=SQLOperate.newInstance().Register(userName, password);
                if(isRegister){
                    System.out.println("注册成功");
                }else{
                    System.out.println("注册失败");
                }
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值