首页 > 代码库 > 第十五周随笔
第十五周随笔
一周完成了设计模式和统一建模的两个大作业,感觉收获颇丰,无论是在编程能力上还是制作UML图上都有了一定的巩固,当然啦,最重要的是加深了自己对数据库和前端界面的一些了解,相信会对小学期的实践有一定的帮助,至于说代码的话,我就贴一下数据库部分的代码吧,因为都是现学现用的,所以有些地方还要理解好。代码如下:
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;
public class ReloadDataBase {
private String DriverName;
private String dbURL;
private String userName;
private String userPwd;
private Connection dbConn;
private Statement stmt;
private ResultSet rs;
public ReloadDataBase(){
DriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=BookShop";
userName="sa";
userPwd="123456";
}
public ReloadDataBase(String DriverName,String dbURL,String userName,String userPwd){
this.DriverName=DriverName;
this.dbURL=dbURL;
this.userName=userName;
this.userPwd=userPwd;
}
public boolean getConnection(){
try{
Class.forName(DriverName);
dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}
public String getDriverName(){
return DriverName;
}
public String getdbURL(){
return dbURL;
}
public String getuserName(){
return userName;
}
public String getuserPwd(){
return userPwd;
}
public void changeDriverName(String DriverName){
this.DriverName=DriverName;
}
public void changedbURL(String dbURL){
this.dbURL=dbURL;
}
public void changeuserName(String userName){
this.userName=userName;
}
public void changeuserPwd(String userPwd){
this.userPwd=userPwd;
}
public ArrayList<Book> getBook(){
String sql="select * from Book";
ArrayList<Book>BookList=new ArrayList<Book>();
if(this.getConnection()==true){
try {
stmt=dbConn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String BookName=rs.getString("BookName");
String Actor=rs.getString("Actor");
String Summary=rs.getString("Summary");
String Picture=rs.getString("Picture");
float Price=rs.getFloat("Price");
String Differ=rs.getString("Differ");
Book book=new Book(BookName,Actor,Summary,Picture,Price,Differ);
BookList.add(book);
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
return BookList;
}
public ArrayList<Order> getOrder(){
String sql="select * from OrderList";
ArrayList<Order>OrderList=new ArrayList<Order> ();
if(this.getConnection()==true){
try{
stmt=dbConn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String Num=rs.getString("Num");
String Data=http://www.mamicode.com/rs.getString("Data");
double Cost=(double)rs.getFloat("Cost");
Scanner BookName=new Scanner(rs.getString("BookList"));
Order temp=new Order(Num,Data,Cost);
BookName.useDelimiter(",");
while(BookName.hasNext()){
String BookNametemp=BookName.next();
temp.addBook(BookNametemp);
}
OrderList.add(temp);
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return OrderList;
}
public ArrayList<Client> getClient(){
String sql="select * from Client";
ArrayList<Client> ClientList=new ArrayList<Client>();
if(this.getConnection()==true){
try{
stmt=dbConn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String ID=rs.getString("ID");
String Name=rs.getString("Name");
String Code=rs.getString("Code");
float Balance=rs.getFloat("Balance");
int VIP=rs.getInt("VIP");
Client client=new Client(ID,Name,Code,Balance,VIP);
Scanner scanner=new Scanner(rs.getString("OrderList"));
scanner.useDelimiter(",");
while(scanner.hasNext()){
String Num=scanner.next();
client.addOrder(Num);
}
ClientList.add(client);
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return ClientList;
}
public void addBook(Book book)throws SQLException{
if(this.getConnection()==true){
String sql="insert into Book(BookName,Actor,Summary,Picture,Price,Differ) values(?,?,?,?,?,?)";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(1,book.getBookName());
ptmt.setString(2, book.getActor());
ptmt.setString(3,book.getSummary());
ptmt.setString(4, book.getPicture());
ptmt.setFloat(5, book.getPrice());
ptmt.setString(6, book.getDiffer());
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public void addClient(Client client)throws SQLException{
if(this.getConnection()==true){
String sql="insert into Client(ID,Name,Code,Balance,VIP,OrderList) values(?,?,?,?,?,?)";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(1, client.getID());
ptmt.setString(2, client.getName());
ptmt.setString(3, client.getCode());
ptmt.setFloat(4, client.getBalance());
ptmt.setInt(5,client.getVIP());
String OrderList="";
for(int i=0;i<client.getOrder().size();i++){
OrderList+=client.getOrder().get(i)+",";
}
ptmt.setString(6, OrderList);
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public void addClientOrder(Client client,String order)throws SQLException{
if(this.getConnection()==true){
String sql="update Client set OrderList=OrderList+? where ID=?";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(1, order+",");
ptmt.setString(2, client.getID());
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public void addOrder(Order order)throws SQLException{
if(this.getConnection()==true){
String sql="insert into OrderList(Num,BookList,Data,Cost) values(?,?,?,?)";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(1, order.getNum());
String BookList="";
for(int i=0;i<order.getBookList().size();i++){
BookList+=order.getBookList().get(i)+",";
}
ptmt.setString(2, BookList);
ptmt.setString(3, order.getData());
ptmt.setFloat(4, (float)order.getCost());
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public int getNum()throws SQLException{
int rowCount=0;
if(this.getConnection()==true){
try{
Statement stmt=dbConn.createStatement();
ResultSet rset=stmt.executeQuery("select count(Num)NumList from OrderList");
if(rset.next()){
rowCount=rset.getInt("NumList");
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return rowCount+1;
}
public void deleteBook(String BookName)throws SQLException{
if(this.getConnection()==true){
String sql="delete from Book where BookName=?";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(1,BookName);
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public void updateCode(String ID,String Code)throws SQLException{
if(this.getConnection()==true){
String sql="update Client set Code=? where ID=?";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(1, Code);
ptmt.setString(2, ID);
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public void updataMoney(String ID,float Cost)throws SQLException{
if(this.getConnection()==true){
String sql="update Client set Balance=Balance-? where ID=?";
PreparedStatement ptmt=dbConn.prepareStatement(sql);
ptmt.setString(2,ID);
ptmt.setFloat(1, Cost);
ptmt.execute();
if(ptmt!=null){
ptmt.close();
ptmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
}
public Boy getBoy(){
Boy Temp=new Boy();
String sql="select * from Book";
if(this.getConnection()==true){
try{
stmt=dbConn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String BookName=rs.getString("BookName");
String Actor=rs.getString("Actor");
String Summary=rs.getString("Summary");
String Picture=rs.getString("Picture");
Float Price=rs.getFloat("Price");
String Differ=rs.getString("Differ");
if(Differ.equals("Boy")){
Book book=new Book(BookName,Actor,Summary,Picture,Price,Differ);
Temp.addBook(book);
}
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return Temp;
}
public Girl getGirl(){
Girl Temp=new Girl();
String sql="select * from Book";
if(this.getConnection()==true){
try{
stmt=dbConn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String BookName=rs.getString("BookName");
String Actor=rs.getString("Actor");
String Summary=rs.getString("Summary");
String Picture=rs.getString("Picture");
Float Price=rs.getFloat("Price");
String Differ=rs.getString("Differ");
if(Differ.equals("Girl")){
Book book=new Book(BookName,Actor,Summary,Picture,Price,Differ);
Temp.addBook(book);
}
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return Temp;
}
public publish getPublish(){
publish Temp=new publish();
String sql="select * from Book";
if(this.getConnection()==true){
try{
stmt=dbConn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next()){
String BookName=rs.getString("BookName");
String Actor=rs.getString("Actor");
String Summary=rs.getString("Summary");
String Picture=rs.getString("Picture");
Float Price=rs.getFloat("Price");
String Differ=rs.getString("Differ");
if(Differ.equals("publish")){
Book book=new Book(BookName,Actor,Summary,Picture,Price,Differ);
Temp.addBook(book);
}
}
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(dbConn!=null){
dbConn.close();
dbConn=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
}
return Temp;
}
}
第十五周随笔