DataSource配置
在webapp下新建META-INF文件夹,在META-INF下新建context.xml文件: 根据自己的数据库信息更改context.xml: 我这是连接本地的mysql,将数据库名称改为你自己的。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/MySQL" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000" initialSize="1"
username="账户名称" password="密码"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/数据库名称?serverTimezone=Asia/Shanghai" />
</Context>
在pom.xml里加入jdbc和servlet的依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<scope>provided</scope>
</dependency>
后端服务
这是后端的目录结构: 对应代码: GetUserSrevlet:
package com.controller;
import com.entity.User;
import com.util.DataSourceUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/getuser")
public class GetUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user = null;
String sql = "select * from user where id=?";
try (Connection conn = DataSourceUtils.getConnection();
PreparedStatement st = conn.prepareStatement(sql)) {
st.setString(1, req.getParameter("uid"));
try (ResultSet rs = st.executeQuery()) {
while (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setInsertTime(rs.getTimestamp("inserttime"));
}
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
System.out.println(user.getName());
}
}
User
package com.entity;
import java.util.Date;
public class User {
private int id;
private String name;
private Date insertTime;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public User(int id, String name, Date insertTime) {
this.id = id;
this.name = name;
this.insertTime = insertTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getInsertTime() {
return insertTime;
}
public void setInsertTime(Date insertTime) {
this.insertTime = insertTime;
}
}
DataSourceUtils
package com.util;
import javax.annotation.Resource;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@WebListener
public class DataSourceUtils implements ServletContextListener {
@Resource(name = "jdbc/MySQL")
private static DataSource dSource;
public static Connection getConnection() throws SQLException {
return dSource.getConnection();
}
}
启动项目,浏览器地址访问,因为没有前端传值,这里模拟请求数据uid=1
http://localhost:8080/DataSource_war/getuser?uid=1
查看终端: 这就是javaweb利用DataSource简单的连接数据库并查询数据。
|