Java连接MySQL的代码

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>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

china-yun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值