目录
模仿天猫商城的J2EE商城网站项目后端3—创建数据库https://blog.csdn.net/qq_37154145/article/details/122523910模仿天猫商城的J2EE商城网站项目后端5——bean包
https://blog.csdn.net/qq_37154145/article/details/122413886为了减少工作量、提高效率,将一些会重复用到的功能,单独提取为类,统一放在工具包util包中。
Dateutil.java
package tmall.util;
/**
* 将从前端获取的日期由text转为Date
* 将Date转为text,以便前端使用
* @author LSB
*/
public class DateUtil {
/**
* Date转为text
* @param d
* @return text
*/
public static java.sql.Timestamp d2t(java.util.Date d) {
if (null == d)
return null;
return new java.sql.Timestamp(d.getTime());
}
/**
* text转为Date
* @param t
* @return Date
*/
public static java.util.Date t2d(java.sql.Timestamp t) {
if (null == t)
return null;
return new java.util.Date(t.getTime());
}
}
DBUtil.java
package tmall.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 数据库连接工具类
* @author LSB
*/
public class DBUtil {
static String ip = "127.0.0.1";
static int port = 3306;
static String database = "tmall";
static String encoding = "UTF-8";
static String loginName = "root";
static String password = "admin";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接
* @return Connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
/**
* 因为本项目时java项目而非动态web项目,所以需要main类作为程序入口。
* 而只要该程序运行起来,就一定需要连接数据库,所以将main放在这个工具类中
* 从而确保程序由入口
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
System.out.println(getConnection());
}
}
ImageUtil.java
package tmall.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
/**
* 图片处理工具类
* @author LSB
*/
public class ImageUtil {
/**
* 将图片转为jpg格式
* @param f
* @return img/null
*/
public static BufferedImage change2jpg(File f) {
try {
Image i = Toolkit.getDefaultToolkit().createImage(f.getAbsolutePath());
PixelGrabber pg = new PixelGrabber(i, 0, 0, -1, -1, true);
pg.grabPixels();
int width = pg.getWidth(), height = pg.getHeight();
final int[] RGB_MASKS = { 0xFF0000, 0xFF00, 0xFF };
final ColorModel RGB_OPAQUE = new DirectColorModel(32, RGB_MASKS[0], RGB_MASKS[1], RGB_MASKS[2]);
DataBuffer buffer = new DataBufferInt((int[]) pg.getPixels(), pg.getWidth() * pg.getHeight());
WritableRaster raster = Raster.createPackedRaster(buffer, width, height, width, RGB_MASKS, null);
BufferedImage img = new BufferedImage(RGB_OPAQUE, raster, false, null);
return img;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 改变图片宽高并存储到硬盘
* @param srcFile
* @param width
* @param height
* @param destFile
*/
public static void resizeImage(File srcFile, int width, int height, File destFile) {
try {
Image i = ImageIO.read(srcFile);
i = resizeImage(i, width, height);
ImageIO.write((RenderedImage) i, "jpg", destFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 改变图片宽高并返回
* @param srcImage
* @param width
* @param height
* @return buffImg/null
*/
public static Image resizeImage(Image srcImage, int width, int height) {
try {
BufferedImage buffImg = null;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
buffImg.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
return buffImg;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Page.java
package tmall.util;
/**
* 分页工具类
* @author LSB
*/
public class Page {
int start;
int count;
int total;
String param;
public Page(int start, int count) {
super();
this.start = start;
this.count = count;
}
/**
* 是否有上一页
* @return boolean
*/
public boolean isHasPreviouse() {
return start != 0;
}
/**
* 是否有下一页
* @return boolean
*/
public boolean isHasNext() {
return start != getLast();
}
/**
* 获取总页数
* @return totalPage
*/
public int getTotalPage() {
int totalPage;
// 假设总数是50,是能够被5整除的,那么就有10页
if (0 == total % count)
totalPage = total / count;
// 假设总数是51,不能够被5整除的,那么就有11页
else
totalPage = total / count + 1;
if (0 == totalPage)
totalPage = 1;
return totalPage;
}
/**
* 获取最后一页第一条记录的下标
* @return
*/
public int getLast() {
int last;
// 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
if (0 == total % count)
last = total - count;
// 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
else
last = total - total % count;
last = last < 0 ? 0 : last;
return last;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
}