数据库、方法封装
public class SQLManger {
private Statement state;
private Connection connection;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public Statement getState() {
return state;
}
public void setState(Statement state) {
this.state = state;
}
public static SQLManger manger;
public static synchronized SQLManger newInstance(){
if(manger==null){
manger=new SQLManger();
}
return manger;
}
private SQLManger(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/clazz";
String user="root";
String password="123456";
try {
Class.forName(driver);
connection=DriverManager.getConnection(url, user, password);
if(!connection.isClosed()){
state=connection.createStatement();
String create="create table if not exists user(id int not null primary key auto_increment ,name varchar(30) not null,password varchar(30) not null)";
state.execute(create);
System.out.println("user表创建成功");
}else{
System.out.println("请打开数据库");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class SQLOperate {
private Connection connection;
public static SQLOperate operate;
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public static synchronized SQLOperate newInstance(){
if(operate==null){
operate=new SQLOperate();
}
return operate;
}
public boolean Login(String name,String password){
connection=SQLManger.newInstance().getConnection();
try {
if(!connection.isClosed()){
PreparedStatement state=connection.prepareStatement("select * from user where name=? and password=?");
state.setString(1, name);
state.setString(2, password);
ResultSet set=state.executeQuery();
set.last();
int num=set.getRow();
System.out.println("查询结果"+num);
return num==1;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean Register(String name,String password){
Statement state=SQLManger.newInstance().getState();
String sql="select * from user where name='"+name+"'";
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(name,password)values('"
+name+"','"
+password
+"')";
state.execute(register);
}
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
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.setTitle("登录");
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(151, 43, 147, 30);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(151, 100, 147, 30);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnLogin = new JButton("login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name=textField.getText();
String password=textField_1.getText();
boolean isLogin=SQLOperate.newInstance().Login(name, password);
if(isLogin){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
}
});
btnLogin.setBounds(178, 162, 93, 23);
contentPane.add(btnLogin);
JLabel lblUsername = new JLabel("username");
lblUsername.setBounds(56, 50, 85, 15);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("password");
lblPassword.setBounds(56, 107, 85, 15);
contentPane.add(lblPassword);
}
}
public class Register 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 {
Register frame = new Register();
frame.setTitle("注册");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
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(131, 30, 148, 34);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(131, 93, 148, 34);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("register");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name=textField.getText();
String password=textField_1.getText();
boolean isRegister=SQLOperate.newInstance().Register(name, password);
if(isRegister){
System.out.println("注册成功");
}else{
System.out.println("注册失败");
}
}
});
btnNewButton.setBounds(155, 169, 93, 23);
contentPane.add(btnNewButton);
JLabel lblUsername = new JLabel("username");
lblUsername.setBounds(39, 39, 82, 15);
contentPane.add(lblUsername);
JLabel lblPasword = new JLabel("pasword");
lblPasword.setBounds(39, 102, 82, 15);
contentPane.add(lblPasword);
}
}
public class Action extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Action frame = new Action();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Action() {
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);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String sql1="insert into user (name,password) values ('qwe','123')";
String sql2="insert into user (name,passwords) values ('we','123')";
String sql3="insert into user (name,password) values ('qw','123')";
Connection conn=SQLManger.newInstance().getConnection();
try {
Statement state=conn.createStatement();
conn.setAutoCommit(false);
state.addBatch(sql1);
state.addBatch(sql2);
state.addBatch(sql3);
state.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
btnNewButton.setBounds(130, 112, 93, 23);
contentPane.add(btnNewButton);
}
}
Serverlet
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) {
e.printStackTrace();
}
return string;
}
}
/**
* Servlet implementation class MyServerlet
*/
@WebServlet("/MyServerlet")
public class MyServerlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServerlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName=request.getParameter("name");
String passWord=request.getParameter("password");
userName=Encoding.doEncoding(userName);
passWord=Encoding.doEncoding(passWord);
System.out.println("用户名:"+userName+" 密码:"+passWord);
String s="用户名:"+userName+" 密码:"+passWord;
response.setHeader("Content-type","text/html;charset=UTF-8");
response.getWriter().append(s);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}