摘要:本文记录在tomcat服务端使用servlet程序与mysql交互,并实现本地与服务端通信的案例
1.准备好环境
1)ubuntu server 14(远程服务器):并配好tomcat9.0、 mysql8.0环境
2)Windows 10本地:eclipse 、mysql8.0、tomcat9.0
3)准备对应(版本和mysql8.0一致)的jdbc包:mysql-connector-java-8.0.13.jar
2.服务端代码和数据库交互操作代码
1)class UserServlet.java
package sqlDemo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UserServlet
*/
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
UserService userService = new UserService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain;charset = utf-8");
response.setHeader("Content-type", "text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
if(userService.login(username, password)) {
response.getOutputStream().write(("登录成功...\n用户: " + username + "\n密码: " + password).getBytes("utf-8"));
}
else {
response.getOutputStream().write(("登录失败...\n请检查用户名和密码是否正确").getBytes("utf-8"));
}
}
}
2)DBUtils.java
package sqlDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil{
private static Connection conn;
//需要设置时区
private static final String URL = "jdbc:mysql://localhost:3306/sqldemo?characterEncoding=utf-8&serverTimezone=UTC";//数据库名字
private static final String USERNAME = "root";//
private static final String PASSWORD = "120331";//
static{
try{
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn(){
try{
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeConn(){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3)UserDao.java
package sqlDemo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDAO {
public boolean queryUser(UserInfo userinfo){
Connection conn = DBUtil.getConn();
String sql="select * from login_table where username=? and password=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, userinfo.getUsername());
ps.setString(2, userinfo.getPassword());
ResultSet rs = ps.executeQuery();
if(rs.next()){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
DBUtil.closeConn();
}
}
}
4)UserInfo.java
package sqlDemo;
public class UserInfo {
private String username;
private String password;
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;
}
}
5)UserService.java
package sqlDemo;
public class UserService {
UserDAO userDAO = new UserDAO();
public boolean login(String username, String password) {
UserInfo userInfo = new UserInfo();
userInfo.setUsername(username);
userInfo.setPassword(password);
return userDAO.queryUser(userInfo);
}
}
测试服务于数据库的操作成功:
浏览器输入地址:
http://localhost:8080/sqlDemo/Login?username=abc&password=123
数据库自行创建
用户查询数据库信息并对比http请求传过来的参数,并返回对比结果,测试成功!
PS:在使用数据库6.0之后的,数据库的连接方式按照我上面方式写
1)URL = "jdbc:mysql://localhost:3306/sqldemo?characterEncoding=utf-8&serverTimezone=UTC";
2)Class.forName("com.mysql.cj.jdbc.Driver");
6.0之前不同于此方法,自行百度。
参考资料:
[1] Android 通过Web服务器与Mysql数据库交互https://blog.csdn.net/Tianweidadada/article/details/79516799