package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JavaRegisterMysql extends HttpServlet{
private static final long serialVersionUID = 1L;
private static final int NAME_CODE_RIGHT = 0; //
private static final int CODE_WRONG = 1; //
private static final int NAME_WRONG = 2; //
String notename = null;
String notepassword = null;
String notephone = null;
public JavaRegisterMysql(){
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
if(req == null){
return;
}
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
String name = req.getParameter("NAME");
String code = req.getParameter("CODE");
String phone = req.getParameter("PHONE");
String chose=req.getParameter("CHOSE");
//手機用戶端通路
int ret=-1;
try {
ret = checkSubmit(name, code, phone, chose);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print(ret);
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
if(req == null){
return;
}
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
String name = req.getParameter("NAME");
String code = req.getParameter("CODE");
String phone = req.getParameter("PHONE");
String chose=req.getParameter("CHOSE");
int ret=-2;
try {
ret = checkSubmit(name, code, phone,chose);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print(ret);
out.flush();
out.close();
}
private int checkSubmit(String name, String code, String phone,String chose) throws Exception{
int ret = -1;
//UserDAOProxy ab=new UserDAOProxy();
User user=new User(name,code,phone);
ret=new UserDAOProxy().findLogin(user, chose);
return ret;
}
}
主接收類
package com;
import java.sql.*;
public class DatabaseConnection {
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/scut";
public static final String USER = "root";
public static final String PASS = "123456";
private Connection con;
public DatabaseConnection(){
try{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PASS);
}
catch(Exception e){}
}
public Connection getConnection(){
return con;
}
public void close(){
try{
if(con!=null){
con.close();
}
}
catch(Exception e){}
}
}
JDBC接口類
package com;
public class UserDAOProxy {
private DatabaseConnection dbc;
private UserDAOImpl idao;
public UserDAOProxy(){
dbc = new DatabaseConnection();
idao = new UserDAOImpl(dbc.getConnection());
}
public int findLogin(User user,String chose)throws Exception{
int flag = idao.findLogin(user,chose);
dbc.close();
return flag;
}
}
package com;
import java.sql.*;
public class UserDAOImpl {
private Connection con;
private Statement stat;
public UserDAOImpl(Connection con){
this.con = con;
}
public int findLogin(User user,String chose)throws Exception{
int flag = 0;
//String sql = "SELECT * FROM noteinfo WHERE name="+"'"+user.getName()+"'";
//String sql = "SELECT * FROM noteinfo WHERE id=1";
//String cd=user.getName();
//String sql = "SELECT * FROM noteinfo WHERE name="+ "'"+user.getName()+"'" +
//" and password="+ "'"+user.getPassword()+"'";
try {
this.stat = con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 結果集
if(chose.equals("1"))
{
flag=1;
String sql = "SELECT * FROM noteinfo WHERE name="+ "'"+user.getName()+"'" +
" and password="+ "'"+user.getPassword()+"'";
//String sql2="insert into noteinfo values(null,'ggg','111','13412323234')";
ResultSet rs = stat.executeQuery(sql);
if(rs.next()){
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
flag = 2;
}
//stat.execute(sql2);
}
if(chose.equals("2"))
{
flag=3;
String sql="insert into noteinfo values(null,"+ "'"+user.getName()+ "'"+","+ "'"+user.getPassword()+ "'"+","
+ "'"+user.getPhone()+ "')";
stat.execute(sql);
flag = 4;
}
return flag;
}
}
package com;
public class User {
public User(){
this.name=" ";
this.password=" ";
this.phone=" ";
}
public User(String name,String code,String phone){
this.name=name;
this.password=code;
this.phone=phone;
}
private String phone;
private String name;
private String password;
public String getPhone(){
return phone;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return password;
}
}
以上為相應調用類
gjwServlet
com.JavaRegisterMysql
gjwServlet
/login
web.xml需添加以上部分代碼