Java连接MySQL的代码
package com.sram.entity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
public static final String DB_URL="jdbc:mysql://localhost:3306/shop";
public static final String DB_USER="root";
public static final String DB_PASS="root";
public static final String DB_DRIVER="com.mysql.jdbc.Driver";
public static Connection connection;
static{
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动加载失败");
}
}
public static Connection getConn(){
try {
connection=DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
} catch (SQLException e) {
System.out.println("数据库连接信息有误");
}
return connection;
}
public static void closeConn(){
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常");
}
}
}
}
从数据库查询资源的代码
public static List<Shop> getAll(){
String sql = "select * from shop";
List shops = new ArrayList<Shop>();
try {
PreparedStatement pst = DBHelper.getConn().prepareStatement(sql);
ResultSet resultSet = pst.executeQuery();
while(resultSet.next()){
shops.add(new Shop(resultSet.getInt("sid"), resultSet.getString("sname"), resultSet.getInt("sprice"), resultSet.getInt("snum"), resultSet.getString("scity"), resultSet.getString("img")));
}
DBHelper.closeConn();
} catch (Exception e) {
}
return shops;
}
servlet的实例代码
public class ShopServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Shop> shops = ShopDIOFactory.getShopDAO().getAll();
System.out.println(shops);
req.setAttribute("shops", shops);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
web.xml的示例代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>NewShopCar</display-name>
<servlet>
<servlet-name>shop</servlet-name>
<servlet-class>com.sram.controler.ShopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>shop</servlet-name>
<url-pattern>/shopServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp示例代码
<body>
<h1>商品展示页面</h1>
<hr/>
<div class="box">
<%
List<Shop> shops = (List<Shop>)request.getAttribute("shops");
for(Shop shop:shops){
%>
<div class="shop">
<a href="shopServlet?action=detail&id=<%=shop.getSid()%>"><img src="img/<%=shop.getImg()%>"/></a>
<div class="font1"><%=shop.getSname()%></div>
<div class="font2">产地:<%=shop.getScity()%></div>
<div class="font3">价格:¥<%=shop.getSprice()%></div>
</div>
<%
}
%>
</div>
</body>