一:写一个bean文件,存放类的表名;(UserBean)
public class UserBean {
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmial() {
return emial;
}
public void setEmial(String emial) {
this.emial = emial;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
private String username; //用户名
private String password; //密码
private int age; //年龄
private String sex; //行别
private String phone; //电话号码
private String emial; //邮箱
private String hobby; //兴趣爱好
private int userid; //用户id
}
二:配置数据库的链接,加载的相关代码(MySqlDbManager)
public class MySqlDbManager {
private static final String URL = "jdbc:mysql://localhost:3306/(数据库名)?useUnicode=true&characterEncoding=UTF-8";
private static final String USER = "root";
private static final String PASSWORD = "..。。。。。";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("加载Mysql数据库驱动失败!");
}
}
/**
* 获取Connection
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("获取数据库连接失败!");
throw e;
}
return conn;
}
/**
* 关闭ResultSet
* @param rs
*/
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}
三:创建接口类,sevelat的http请求接口。(JsonServlet)
public class JsonServlet extends HttpServlet{
private String jsonString;
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
jsonString = "Error!";
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter outss = response.getWriter();
String contextPath = request.getContextPath();
String actionString = request.getParameter("action");
if(actionString.equals("loginUser")){
String username =new String(request.getParameter("username").getBytes("UTF-8"), "UTF-8");
String password =new String(request.getParameter("password").getBytes("UTF-8"), "UTF-8");
jsonString = LoginUser( username, password);//这里传递username,password
}
}
/**
* 用户登录
*/
private String LoginUser(String username,String password){
return UserBeanHelp.SelectUserBean(username, password);
}
四:创建UserBeanHelp类
public class UserBeanHelp {
//private HttpServletRequest request;
//private HttpServletResponse response;
private static int code;
private static UserBeanUtil ubu;
private static String jsonString;
private static List<UserBean> list = new ArrayList<>();
private final static int SUCCESS = 0x200; //成功
private final static int ERROR = 0x001; //失败
private static UserBeanJson ubj;
public static String SelectUserBean(String username,String password){
try{
ubu = new UserBeanUtil();
ubj = new UserBeanJson();
list = ubu.getUserBean(username, password);//这个是进入数据库的查询,返回list数据
}catch (Exception e) {
e.printStackTrace();
}
if(list.size()>0){
code = SUCCESS;
jsonString = ubj.SelectUserBean(list,code);
}else{
code = ERROR;
jsonString = ubj.SelectUserBean(list,code);
}
return jsonString;
}
}
五:创建UserBeanUtil类
public class UserBeanUtil implements UserDao{
/**
* 用户登录接口
*/
@Override
public List<UserBean> getUserBean(String username, String password) throws SQLException {
List<UserBean> list = new ArrayList<UserBean>();
String sql="select * from tb_user where username="+"'"+username+"'"+" and password="+"'"+password+"'";
try {
Connection conn = MySqlDbManager.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
UserBean ub = new UserBean();
ub.setUserid(rs.getInt("id"));
ub.setUsername(rs.getString("username"));
ub.setPassword(rs.getString("password"));
ub.setAge(rs.getInt("age"));
ub.setSex(rs.getString("sex"));
ub.setPhone(rs.getString("phone"));
ub.setEmial(rs.getString("emial"));
ub.setHobby(rs.getString("hobby"));
list.add(ub);
}
MySqlDbManager.closeConnection(rs, stmt, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
import java.sql.SQLException;
import java.util.List;
import com.hs.bean.UserBean;
public interface UserDao {
/**
* 获取当前用户的信息列表
* @param username
* @param password
* @return
* @throws SQLException
*/
public List<UserBean> getUserBean(String username,String password) throws SQLException;
/**
* 查询数据库中是否有对应的UserName,如果有,返回对应id,没有,返回0
*
* @param connection
* @param userName
* @return
*/
public int queryUserName(String userName) throws SQLException;
}
查询到了数据,会返回数据到list里面,因此,首先看这数据库的配置,链接,初始化是否成功了,不然后面的也是白塔。
if(list.size()>0){
code = SUCCESS;
jsonString = ubj.SelectUserBean(list,code);
}else{
code = ERROR;
jsonString = ubj.SelectUserBean(list,code);
}
return jsonString;
这里就是返回数据库里的信息,到这里来进行判断,然后转换为json的数据格式,进行在接口上展示,是对象还是数组等;
六:创建UserBeanJson类,实现UserBeanTool方法
public class UserBeanJson implements UserBeanTool{
//成功和失败的 状态
private final static int SUCCESS = 0x200;
private final static int ERROR = 0x001;
//private final static int LIKE=0x300;
//private static String message = "Error";
private final static String SUCCESSCODE = "success";
private final static String ERRORCODE = "error";
private final static String LIKECODE = "用户被注册";
/**
* 查询数据
*/
@Override
public String SelectUserBean(Object value, int code) {
JSONObject jsonObject=new JSONObject();
if(code == SUCCESS){
jsonObject.put("code", "200");
jsonObject.put("message", SUCCESSCODE);
}else if(code == ERROR){
jsonObject.put("code", "0");
jsonObject.put("message", ERRORCODE);
}
jsonObject.put("user", value);
return jsonObject.toString();
}
}
public interface UserBeanTool {
/**
* 查询数据接口
* @param value
* @param code
* @return
*/
public String SelectUserBean(Object value,int code);
/**
* 插入数据接口
* @param value
* @param code
* @return
*/
public String InsertUserBean(Object value,int code);
}
七,返回的jsonObject就会在sevelat登陆的post里,看到数组信息,所有的也就这些了。没有其他的了,跟着我的一步步来,绝对是可以的,网上很多sevelat接口这块很多坑,不是这里问题,就是那里问题。
记住,如果你在开发json中,有什么问题或者不懂的,可以咨询我,我看到会及时回复的,会尽最大的努力帮助到大家,让更多的人,少走些弯路。
可以点击这个进行测试了。之前链接上tomcat的,我们就根据自己的版本,8.0或者9.0进行运行,看看程序的接口如何,我这里是8.5的tomcat;
八:最终的结果,所有的心酸,和遇到的坑,都归根于此,终于实现了。
总结:
1,千万要注意mysql数据库链接的配置,得根据jar版本配置,如果jar包是8.0之前的就用官方有列子,是8.0或以后的版本,就是按照我上面那个数据库类链接/初始化,这是官方的java链接mysql:https://www.runoob.com/java/java-mysql-connect.html
2,如果报错404,一般是路径的原因导致,以及web.xml文件的配置路径,或者检查下tomcat的运行配置,但可以根据下面那个报错英文的信息,百度找找看;3,如果报错的500,这个原因导致的涉及太多,因为我也不清楚你那个错误代码是什么,但这个也容易解决,网上也有很多关于500错误的解决方法,可以慢慢尝试下,也阐述了很多方法。