📄 databaseinit.java
字号:
package dao;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DataBaseInit {
/**
* 插入文本数据。
* @throws IOException
*/
public static void insertvalues()throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(DataBaseInit.class.getResourceAsStream("insertvalues.sql")));
String temp=null;
try {
System.out.println("正在插入商品信息,请稍等...");
while((temp=br.readLine())!=null){
if(!temp.equals("")&&!temp.startsWith("--")){
DBCon.execute(temp.substring(0,temp.length()-1));
}
}
System.out.println("商品信息插入完毕。");
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
/**
* 插入商品的图片。
* @throws SQLException
* @throws IOException
*/
public static void productPicInit()throws SQLException,IOException{
StringBuffer sql=new StringBuffer();
sql.append("insert into photo (pid,image) values (?,?)");
try {
System.out.println("正在插入商品图片,请稍等...");
for(int i=1;i<100;i++){
PreparedStatement ps=DBCon.getCon().prepareStatement(sql.toString());
InputStream is=DataBaseInit.class.getResourceAsStream("/pics/"+(i%20+1)+".png");
ps.setInt(1, i);
ps.setBinaryStream(2, is, is.available());
ps.execute();
}
System.out.println("商品图片插入完毕。");
} catch (SQLException sqlexception) {
sqlexception.printStackTrace();
throw sqlexception;
} catch (IOException ioexception) {
ioexception.printStackTrace();
throw ioexception;
}
}
/**
* 插入分类图片
* @throws SQLException
* @throws IOException
*/
public static void categoryPicInit() throws SQLException,IOException{
StringBuffer sql=new StringBuffer();
sql.append("update category set photo=? where id=? ");
try {
System.out.println("正在插入分类图片,请稍等...");
for(int i=1;i<99;i++){
PreparedStatement ps=DBCon.getCon().prepareStatement(sql.toString());
InputStream is=DataBaseInit.class.getResourceAsStream("/categorypic/"+(i%20+1)+".gif");
ps.setBinaryStream(1, is, is.available());
ps.setInt(2, i);
ps.execute();
}
System.out.println("商品分类图片插入完毕。");
} catch (SQLException sqlexception) {
sqlexception.printStackTrace();
throw sqlexception;
} catch (IOException ioexception) {
ioexception.printStackTrace();
throw ioexception;
}
}
public static void bigPicInit() throws SQLException,IOException{
int[] products={
64,67,52,53, 65,66,69,73, 68,70,71,72,
51,55,76, 87,50,12,24, 88,83,26,43};
StringBuffer sql=new StringBuffer();
sql.append("update photo set image=? where id=? ");
try {
System.out.println("正在插入商品大图,请稍等...");
for(int i=0;i<products.length;i++){
PreparedStatement ps=DBCon.getCon().prepareStatement(sql.toString());
InputStream is=DataBaseInit.class.getResourceAsStream("/bigpic/"+(i+1)+".jpg");
ps.setBinaryStream(1, is, is.available());
ps.setInt(2, products[i]);
ps.execute();
}
System.out.println("商品大图插入完毕。");
} catch (SQLException sqlexception) {
sqlexception.printStackTrace();
throw sqlexception;
} catch (IOException ioexception) {
ioexception.printStackTrace();
throw ioexception;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -