sql:
drop table BookKindList;
#书目录
create table BookKindList
(
BookKindID INT NOT NULL AUTO_INCREMENT, #自动增加
BookKindName nvarchar(500) not null,
BookKindParent int null,
PRIMARY KEY(BookKindID) #主键
);
#删除
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`DeleteBookKind` $$
CREATE PROCEDURE `geovindu`.`DeleteBookKind` (IN param1 INT)
BEGIN
Delete From bookkindlist WHERe BookKindID = param1;
END $$
DELIMITER ;
delete from bookkindlist WHERe BookKindID =10;
SELECt * FROM bookkindlist;
execute DeleteBookKind(10);
#查询所有
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindListAll` $$
CREATE PROCEDURE `geovindu`.`proc_Select_BookKindListAll` ()
BEGIN
SELECT * FROM bookkindlist;
END $$
DELIMITER ;
DROp PROCEDURE proc_Select_BookKindListAll;
select * from `geovindu`.`bookkindlist`;
SELECt * FROM bookkindlist;
#统计
DELIMITER $$
DROp PROCEDURE IF EXISTS `geovindu`.`BookKindCount` $$
CREATE PROCEDURE `geovindu`.`BookKindCount` (OUT param1ID INT)
BEGIN
select COUNT(*) into param1ID From bookkindlist;
END $$
DELIMITER ;
#更新
DELIMITER $$
DROp PROCEDURE IF EXISTS `geovindu`.`proc_Update_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Update_BookKindList` (IN param1ID Int,IN param1Name NVarChar(1000),IN param1Parent Int)
BEGIN
IF NOT EXISTS (SELECT * FROM BookKindList WHERe BookKindName=param1Name) then #如果存在相同的记录,不更新名称
UPDATe BookKindList
SET
BookKindName=param1Name ,
BookKindParent=param1Parent
where
BookKindID=param1ID;
ELSE
UPDATE BookKindList
SET BookKindParent=param1Parent
where
BookKindID=param1ID;
END IF;
END $$
DELIMITER ;
#查询一条
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Select_BookKindList` (IN param1 INT)
BEGIN
SELECT * FROM BookKindList WHERe BookKindID = param1;
END $$
DELIMITER ;
#插入一条
DELIMITER $$
DROp PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindList` (IN param1Name NVarChar(1000),IN param1Parent Int)
BEGIN
insert into BookKindList(BookKindName,BookKindParent) values(param1Name,param1Parent);
END $$
DELIMITER ;
#插入一条返回值
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $$
CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindOut` (IN param1Name NVarChar(1000),IN param1Parent Int,OUT ID INT)
BEGIN
IF NOT EXISTS (SELECT * FROM BookKindList WHERe BookKindName=param1Name) then #如果存在相同的记录,不添加
INSERT INTO BookKindList (BookKindName,BookKindParent)VALUES(param1Name ,param1Parent);
#set ID=Last_insert_id()
SELECt LAST_INSERT_ID() into ID;
end if;
END $$
DELIMITER ;
MODEL:
package Geovin.Model;
public class BookKind {
//
private int bookKindID;
private String bookKindName;
private int bookKindParent;
public int getBookKindID() {
return bookKindID;
}
public void setBookKindID(int bookKindID) {
this.bookKindID = bookKindID;
}
public String getBookKindName() {
return bookKindName;
}
public void setBookKindName(String bookKindName) {
this.bookKindName = bookKindName;
}
public int getBookKindParent() {
return bookKindParent;
}
public void setBookKindParent(int bookKindParent) {
this.bookKindParent = bookKindParent;
}
}
DAL
//#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
package Geovin.DAL;
import java.awt.print.Book;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetmetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.sql.*;
import java.lang.reflect.Parameter;
import Geovin.Model.*;
import Geovin.UtilitieDB.DuMySqlHelper;
public class GeovinDuDAL {
DuMySqlHelper duMySqlHelperr=new DuMySqlHelper();
public Boolean AddSql(BookKind bookKind)
{
Boolean isok=false;
String sql="INSERT INTO BookKindList(BookKindName,BookKindParent) values(? ,?)";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindName());
duParameter.setDataType("String");
duParameters.add(duParameter);
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindParent());
duParameter.setDataType("int");
duParameters.add(duParameter);
isok=duMySqlHelperr.ExecuteInsertSql(sql,duParameters);
return isok;
}
public Boolean AddProc(BookKind bookKind)
{
Boolean isok=false;
String sql="{CALL proc_Insert_BookKindList(? ,?)}";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindName());
duParameter.setDataType("String");
duParameters.add(duParameter);
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindParent());
duParameter.setDataType("int");
duParameters.add(duParameter);
isok=duMySqlHelperr.ExecuteInsert(sql,duParameters);
return isok;
}
public Boolean AddProc(BookKind bookKind,int outValue)
{
Boolean isok=false;
String sql="{CALL proc_Insert_BookKindList(? ,?)}";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindName());
duParameter.setDataType("String");
duParameters.add(duParameter);
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindParent());
duParameter.setDataType("int");
duParameters.add(duParameter);
isok=duMySqlHelperr.ExecuteInsertOutSingleInt(sql,duParameters,outValue);
return isok;
}
public int EditSQL(BookKind bookKind)
{
int isok=0;
String sql="UPDATE BookKindList SET BookKindName=?,BookKindParent=? where BookKindID=?";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindName());
duParameter.setDataType("String");
duParameters.add(duParameter);
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindParent());
duParameter.setDataType("int");
duParameters.add(duParameter);
duParameter.setParameterValue(bookKind.getBookKindID());
duParameter.setDataType("int");
duParameters.add(duParameter);
isok=duMySqlHelperr.ExecuteUpdateSql(sql,duParameters);
return isok;
}
public int EditProc(BookKind bookKind)
{
int isok=0;
String sql="CALL proc_Update_BookKindList(?,?,?)";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindID());
duParameter.setDataType("int");
duParameters.add(duParameter);
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindName());
duParameter.setDataType("String");
duParameters.add(duParameter);
duParameter=new DuParameter();
duParameter.setParameterValue(bookKind.getBookKindParent());
duParameter.setDataType("int");
duParameters.add(duParameter);
isok=duMySqlHelperr.ExecuteUpdate(sql,duParameters);
return isok;
}
public int DelSQL(int id) {
int isok=0;
String sql="Delete From bookkindlist WHERe BookKindID =?";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(id);
duParameter.setDataType("int");
duParameters.add(duParameter);
isok= duMySqlHelperr.ExecuteDeletSql(sql,duParameters);
return isok;
}
public int DelProc(int id) {
int isok=0;
String sql="{CALL DeleteBookKind(?)}";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(id);
duParameter.setDataType("int");
duParameters.add(duParameter);
isok= duMySqlHelperr.ExecuteDelte(sql,duParameters);
return isok;
}
public BookKind selectSQL(int id)
{
ResultSet resultSet=null;
BookKind bookKind=null;
String sql = "SELECT * FROM BookKindList where BookKindID=?";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(id);
duParameter.setDataType("int");
duParameters.add(duParameter);
try {
resultSet = duMySqlHelperr.ExecuteQuery(sql, duParameters);
while (resultSet.next()) {
bookKind =new BookKind();
bookKind.setBookKindID(resultSet.getInt("BookKindID"));
bookKind.setBookKindName(resultSet.getString("BookKindName"));
bookKind.setBookKindParent(resultSet.getInt("BookKindParent"));
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return bookKind;
}
public BookKind selectProc(int id)
{
ResultSet resultSet=null;
BookKind bookKind=null;
String sql = "{CALL proc_Select_BookKindList(?)}";
ArrayList duParameters=new ArrayList();
DuParameter duParameter=null;
duParameter=new DuParameter();
duParameter.setParameterValue(id);
duParameter.setDataType("int");
duParameters.add(duParameter);
try {
resultSet = duMySqlHelperr.ExecuteQuery(sql, duParameters);
while (resultSet.next()) {
bookKind =new BookKind();
bookKind.setBookKindID(resultSet.getInt("BookKindID"));
bookKind.setBookKindName(resultSet.getString("BookKindName"));
bookKind.setBookKindParent(resultSet.getInt("BookKindParent"));
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return bookKind;
}
public ArrayList selectAllSQL()
{
ArrayList list=new ArrayList();
ResultSet resultSet=null;
BookKind bookKind=null;
String sql = "SELECT * FROM BookKindList";
try {
resultSet = duMySqlHelperr.ExecuteQuery(sql, null);
while (resultSet.next()) {
bookKind =new BookKind();
bookKind.setBookKindID(resultSet.getInt("BookKindID"));
bookKind.setBookKindName(resultSet.getString("BookKindName"));
bookKind.setBookKindParent(resultSet.getInt("BookKindParent"));
list.add(bookKind);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return list;
}
public ArrayList selectAllProc()
{
ArrayList list=new ArrayList();
ResultSet resultSet=null;
BookKind bookKind=null;
String sql = "{CALL proc_Select_BookKindListAll()}";
try {
resultSet = duMySqlHelperr.ExecuteQuery(sql, null);
while (resultSet.next()) {
bookKind =new BookKind();
bookKind.setBookKindID(resultSet.getInt("BookKindID"));
bookKind.setBookKindName(resultSet.getString("BookKindName"));
bookKind.setBookKindParent(resultSet.getInt("BookKindParent"));
list.add(bookKind);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return list;
}
}
IDAL:
package Geovin.Interface;
import Geovin.Model.BookKind;
import java.util.ArrayList;
public interface BookKindInterface {
public int Add(BookKind info);
public int AddOut(BookKind info);
public int Update(BookKind info);
public BookKind SelectSQLBookKindInfo(String id);
public ArrayList SelectSQLBookKindAll();
}
Factory:
package Geovin.Factory;
import Geovin.DAL.BookKindDAL;
import Geovin.Interface.BookKindInterface;
public class AbstractFactory {
public static BookKindInterface CreateBookKind()
{
BookKindInterface iBookKindInterface=new BookKindDAL();
return iBookKindInterface;
}
}
BLL:
package Geovin.BLL;
import Geovin.Model.*;
import Geovin.Factory.AbstractFactory;
import Geovin.Interface.*;
import java.util.ArrayList;
public class BookKindBLL {
private static BookKindInterface dal=AbstractFactory.CreateBookKind();
public int Add(BookKind info)
{
return dal.Add(info);
}
public int AddOut(BookKind info)
{
return dal.AddOut(info);
}
public int Update(BookKind info)
{
return dal.Update(info);
}
public BookKind SelectSQLBookKindInfo(String id)
{
return dal.SelectSQLBookKindInfo(id);
}
public ArrayList SelectSQLBookKindAll()
{
return dal.SelectSQLBookKindAll();
}
}
测试:
//CustomerDAL dal=new CustomerDAL();
//dal.SelectSQLCustomer("1");
// BookKindDAL dal=new BookKindDAL();
BookKindBLL dal=new BookKindBLL();
BookKind info=dal.SelectSQLBookKindInfo("1");
System.out.println("tn实体读出:id-"+info.getBookKindID()+";类目名称:"+info.getBookKindName()+";父节点ID:"+info.getBookKindParent());
BookKind newinfo=new BookKind();
newinfo.setBookKindID(5);
newinfo.setBookKindName("聚文小说");
newinfo.setBookKindParent(2);
int ok=dal.Update(newinfo);
if(ok>0) {
System.out.println("更新记录,ok"+String.valueOf(ok));
}
else
{
System.out.println("更新不成功,no");
}
info=new BookKind();
info=dal.SelectSQLBookKindInfo("5");
System.out.println("tn"+info.getBookKindName());



