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);