package com.phone.jdbc;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 写一个程序让用户查询手机列表,用户输入页码,展示相应分页的手机信息(每页大小10)
* @author Phone
*
*/
public class Phone {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("****************查询页码*****************");
System.out.println("请输入页码:");
int page = sc.nextInt();
String sql ="select * from phone";
ResultSet rs = showPage(sql, 10, page);
while(rs.next()) {
System.out.println(rs.getInt("pid")+"\t" + rs.getString("pmodel") + "\t"+rs.getInt("price")+"\t" +rs.getInt("pstore"));
}
System.out.println("***************下单******************");
System.out.println("请输入手机id:");
int pid = sc.nextInt();
System.out.println("请输入数量:");
int amount = sc.nextInt();
placeOrder(pid,amount);
System.out.println("****************写入xls文件中*****************");
xls(sql);
}
/**
* 获取分页
* @param querySql 查询的sql
* @param pageSize 分页大小
* @param page 当前页码
*/
public static ResultSet showPage(String querySql, int pageSize, int page) {
int total = getTotal(querySql); //总记录数
//总页数
int totalPage = total%pageSize==0 ? total/pageSize : total/pageSize+1;
System.out.println("总页数:" + totalPage);
//计算起始行号和结束行号
int start = pageSize*(page-1)+1;
int end = start+pageSize;
//分页的sql
String sql = "select * from " +
" (select rownum rn, t.* from " +
" ("+ querySql +") t " +
" ) tt" +
" where tt.rn>=? and tt.rn<?";
Connection conn = DBUtil.getConnection();
PreparedStatement ps;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//获取总记录数
private static int getTotal(String querySql) {
Connection conn = DBUtil.getConnection();
PreparedStatement ps;
int total = 0;
try {
ps = conn.prepareStatement("select count(*) from ("+ querySql +") t");
ResultSet rs = ps.executeQuery();
if(rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
//3. 写一个程序提供下单功能,用户输入手机的id和数量,生成订单。注意库存,事务控制
public static void placeOrder(int phoneid,int amount) {
Connection conn = DBUtil.getConnection();
String sql = "insert into orders values(seq.nextval,?,?,?)";
String sqlstr = "select price,pstore from phone where pid =?";
String updateSQL = "update phone set pstore=? where pid=?";
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement(sqlstr);
pst.setInt(1, phoneid);
PreparedStatement ps1 = conn.prepareStatement(updateSQL);
ResultSet rs = pst.executeQuery();
ps = conn.prepareStatement(sql);
if(rs.next()) {
ps.setInt(1, phoneid);
ps.setInt(2, amount);
if(amount>0 && amount < rs.getInt("pstore")) {
ps.setInt(3, rs.getInt("price")*amount);
}else {
System.out.println("该手机库存不足!");
return;
}
if(rs.getInt("pstore") > amount) {
ps1.setInt(1, rs.getInt("pstore")-amount);
}else {
System.out.println("该手机库存不足!");
return;
}
ps1.setInt(2, phoneid);
System.out.println("下单完成!");
ps.executeUpdate();
ps1.executeUpdate();
conn.commit(); //手动提交
conn.setAutoCommit(true); //恢复自动提交
}else {
System.out.println("输入的手机id不正确!");
return;
}
} catch (SQLException e) {
try {
System.out.println("出现错误,数据回滚");
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
DBUtil.close(ps);
}
}
public static void xls(String querySql) {
try {
File file = new File("D://phone.xls");
if (!file.exists()) {
file.createNewFile();
}
WritableWorkbook wwb = Workbook.createWorkbook(file);
WritableSheet ws = wwb.createSheet("phone", 1);
Label lable1 = new Label(0, 0, "pid");
Label lable2 = new Label(1, 0, "pmodel");
Label lable3 = new Label(2, 0, "price");
Label lable4 = new Label(3, 0, "pstore");
ws.addCell(lable1);
ws.addCell(lable2);
ws.addCell(lable3);
ws.addCell(lable4);
Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(querySql);
ResultSet rs = ps.executeQuery();
int total = getTotal(querySql); //总记录数
for (int i = 1; i <= total; i++) {
if(rs.next()) {
ws.addCell(new Number(0,i,rs.getInt("pid")));
ws.addCell(new Label(1,i,rs.getString("pmodel")));
ws.addCell(new Number(2,i,rs.getInt("price")));
ws.addCell(new Number(3,i,rs.getInt("pstore")));
}
}
wwb.write();//写入文件
wwb.close();//关闭流
System.out.println("文件信息写入成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
页的类和连接数据库的类在前几篇文章中,不再重复写!