jsp网站实现收藏功能,并把数据写入到mysq

代码MVC设计模式,前台数据发送到servlet类里面进行一系列的数据操作。

字段名 类型 约束 其他
userId int foreign key primary key联合主键
artworkId int foreign key
collectTime date
collect_or_not int check(0,1) 默认为0,取值范围0,1;0表示用户没有收藏;1表示该用户已经收藏此信息
## BaseDAO.java 实现数据库连接和资源关闭
package com.dao;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.Callable;

import javax.sound.midi.VoiceStatus;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
//实现数据连接和资源关闭操作
public class BaseDAO {
	public static Connection getConnection(){
		try {
			System.out.println("连接数据库线程启动");
			return new BaseCallableGetConnection().call();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}
	}
	//资源关闭操作
	public static void close(java.sql.Connection con, PreparedStatement preparedStatement, ResultSet rs){
		System.out.println("资源关闭线程启动");
		Thread threadClose = new BaseThreadClose(con, preparedStatement, rs);
	}
}

//连接数据库线程类
class BaseCallableGetConnection implements Callable<Connection>{
	private static final String driver = "com.mysql.jdbc.Driver";
	private static final String url = "jdbc:mysql://127.0.0.1:3306/famousmall";
	private static final String user = "root";
	private static final String password = "123456";
	@Override
	public Connection call() throws Exception {
		Class.forName(driver);
		Connection connection = (Connection) DriverManager.getConnection(url, user, password);
		return connection;
	}
}

//关闭数据库资源线程类
class BaseThreadClose extends Thread{
	private Connection connection;
	private PreparedStatement pstmt;
	private ResultSet resultSet;
	public BaseThreadClose(java.sql.Connection con, PreparedStatement pstmt, ResultSet resultSet){//构造函数
		this.connection = (Connection) con;
		this.pstmt = pstmt;
		this.resultSet = resultSet;
	}
	@Override
	public voi
  • 10
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值