[size=x-large][/size][color=darkblue][/color]第一种写法:

publicclassStu{

privateintid;

privateStringname;

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStu(intid,Stringname){

super();

this.id=id;

this.name=name;

}

publicStu(){}

}

importjava.sql.SQLException;

importjava.util.ArrayList;

publicinterfaceStuDeal{

publicvoidinsert(Stustu)throwsSQLException;

publicvoiddelete(intid)throwsSQLException;

publicvoidquery(Stustu)throwsSQLException;

publicArrayListfindCondition(Stustu)throwsSQLException;

}

/*

*关闭各种资源的工具类

*

***/

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.Statement;

importjava.sql.ResultSet;

publicclassUtil{

publicstaticConnectiongetConnection(){

Connectionconn=null;

try{

Class.forName("com.mysql.jdbc.Driver");

Stringurl="jdbc:mysql://localhost:3306/test";

conn=DriverManager.getConnection(url,"root","1");}catch(Exceptione){e.printStackTrace();}

returnconn;

}

}

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.ArrayList;

publicclassStuDaoimplementsStuDeal{

publicvoidinsert(Stustu)throwsSQLException{

Connectionconn=Util.getConnection();

PreparedStatementps=conn

.prepareStatement("insertintostuvalues(?,?)");

ps.setInt(1,stu.getId());

ps.setString(2,stu.getName());

ps.executeUpdate();

}

publicvoiddelete(intid)throwsSQLException{

Connectionconn=Util.getConnection();

PreparedStatementps=conn

.prepareStatement("deletefromstuwhereid=?");

ps.setInt(1,id);

ps.executeUpdate();

}

publicvoidquery(Stustu)throwsSQLException{

Connectionconn=Util.getConnection();

PreparedStatementps=conn

.prepareStatement("select*fromstuwhereid=?");

ps.setInt(1,stu.getId());

ResultSetrs=ps.executeQuery();

while(rs.next()){

System.out.println("id号为:"+rs.getInt(1)+""+"名字为:"

+rs.getString(2));

}

sta.close();

rs.close();

conn.close();

}

publicArrayListfindCondition(Stustu)throwsSQLException{

ArrayList<Stu>s=newArrayList();

Stringsql="selectid,namefromstuwhere1=1and";

if(stu.getId()!=0){

sql=sql+"id="+stu.getId()+"and";

}

if(stu.getName()!=null){

sql=sql+"name='"+stu.getName()+"'and";

}

if(sql.endsWith("and")){

sql=sql.substring(0,sql.length()-3);

}

Connectionconn=Util.getConnection();

Statementsta=conn.createStatement();

ResultSetrs=sta.executeQuery(sql);

while(rs.next()){

Stust=newStu();

st.setId(rs.getInt(1));

st.setName(rs.getString(2));

s.add(st);

}

sta.close();

rs.close();

conn.close();

returns;

}

}

importjava.sql.SQLException;

importjava.util.ArrayList;

publicclassStuTest{

/**

*@paramargs

*@throwsSQLException

*/

publicstaticvoidmain(String[]args)throwsSQLException{

//TODOAuto-generatedmethodstub

/*Stus=newStu();

s.setName("d");

StuDealsdao=newStuDao();

ArrayList<Stu>list=sdao.findCondition(s);

for(inti=0;i<list.size();i++){

System.out.println("学生id为:"+list.get(i).getId()+"学生姓名为:"+list.get(i).getName());

}*/

StuDealsdao=newStuDao();

Stus=newStu(9,"c");

sdao.insert(s);

}

}

第二种写法

publicclassStu{

privateintid;

privateStringname;

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStu(intid,Stringname){

super();

this.id=id;

this.name=name;

}

publicStu(){}

}

importjava.sql.SQLException;

importjava.util.ArrayList;

publicinterfaceStuDeal{

publicvoidinsert(Stustu)throwsSQLException;

publicvoiddelete(intid)throwsSQLException;

publicvoidquery(Stustu)throwsSQLException;

publicArrayListfindCondition(Stustu)throwsSQLException;

}

/*

*关闭各种资源的工具类

*

***/

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.Statement;

importjava.sql.ResultSet;

publicclassUtil{

publicstaticConnectiongetConnection(){

Connectionconn=null;

try{

Class.forName("com.mysql.jdbc.Driver");

Stringurl="jdbc:mysql://localhost:3306/test";

conn=DriverManager.getConnection(url,"root","1");}catch(Exceptione){e.printStackTrace();}

returnconn;

}

}

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.ArrayList;

publicclassStuDaoimplementsStuDeal{

publicvoidinsert(Stustu)throwsSQLException{

Connectionconn=Util.getConnection();

Statementsta=conn.createStatement();

Stringsql="insertintostuvalues("+stu.getId()+","+"'"+stu.getName()+"'"+")";

sta.executeUpdate(sql);

sta.close();

conn.close();

}

publicvoiddelete(intid)throwsSQLException{

Connectionconn=Util.getConnection();

Statementsta=conn.createStatement();

sta.executeUpdate("deletefromstuwhereid="+id);

sta.close();

conn.close();

}

publicvoidquery(Stustu)throwsSQLException{

Connectionconn=Util.getConnection();

Statementsta=conn.createStatement();

ResultSetrs=sta.executeQuery("select*fromstuwhereid="+stu.getId());

while(rs.next()){

System.out.println("id号为:"+rs.getInt(1)+""+"名字为:"

+rs.getString(2));

}

sta.close();

rs.close();

conn.close();

}

publicArrayListfindCondition(Stustu)throwsSQLException{

ArrayList<Stu>s=newArrayList();

Stringsql="selectid,namefromstuwhere1=1and";

if(stu.getId()!=0){

sql=sql+"id="+stu.getId()+"and";

}

if(stu.getName()!=null){

sql=sql+"name='"+stu.getName()+"'and";

}

if(sql.endsWith("and")){

sql=sql.substring(0,sql.length()-3);

}

Connectionconn=Util.getConnection();

Statementsta=conn.createStatement();

ResultSetrs=sta.executeQuery(sql);

while(rs.next()){

Stust=newStu();

st.setId(rs.getInt(1));

st.setName(rs.getString(2));

s.add(st);

}

sta.close();

rs.close();

conn.close();

returns;

}

}

importjava.sql.SQLException;

importjava.util.ArrayList;

publicclassStuTest{

/**

*@paramargs

*@throwsSQLException

*/

publicstaticvoidmain(String[]args)throwsSQLException{

//TODOAuto-generatedmethodstub

/*Stus=newStu();

s.setName("d");

StuDealsdao=newStuDao();

ArrayList<Stu>list=sdao.findCondition(s);

for(inti=0;i<list.size();i++){

System.out.println("学生id为:"+list.get(i).getId()+"学生姓名为:"+list.get(i).getName());

}*/

StuDealsdao=newStuDao();

Stus=newStu(9,"c");

sdao.insert(s);

}

}

第三种写法

importjava.sql.Connection;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

publicclassStuFactory{

privatestaticConnectionconn=null;

privatestaticStatementsta=null;

privatestaticResultSetrs=null;

publicstaticStatementgetStatement()throwsSQLException{

conn=Util.getConnection();

sta=conn.createStatement();

returnsta;

}

publicstaticvoidcloseStu(Statementsta){

if(sta!=null){try{

sta.close();

}catch(Exceptione){

e.printStackTrace();

}

}

if(conn!=null){try{

conn.close();

}catch(Exceptione){

e.printStackTrace();

}

}

}

publicstaticvoidcloseStu(Statementsta,ResultSetrs){

if(sta!=null){try{

sta.close();

}catch(Exceptione){

e.printStackTrace();

}

}

if(rs!=null){try{

rs.close();

}catch(Exceptione){

e.printStackTrace();

}

}

if(conn!=null){try{

conn.close();

}catch(Exceptione){

e.printStackTrace();

}

}

}

}

importjava.sql.SQLException;

importjava.util.ArrayList;

publicinterfaceStuDeal{

publicvoidinsert(Stustu)throwsSQLException;

publicvoiddelete(intid)throwsSQLException;

publicvoidquery(Stustu)throwsSQLException;

publicArrayListfindCondition(Stustu)throwsSQLException;

}

publicclassStu{

privateintid;

privateStringname;

publicintgetId(){

returnid;

}

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStu(intid,Stringname){

super();

this.id=id;

this.name=name;

}

publicStu(){}

}

/*

*关闭各种资源的工具类

*

***/

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.Statement;

importjava.sql.ResultSet;

publicclassUtil{

publicstaticConnectiongetConnection(){

Connectionconn=null;

try{

Class.forName("com.mysql.jdbc.Driver");

Stringurl="jdbc:mysql://localhost:3306/test";

conn=DriverManager.getConnection(url,"root","1");}catch(Exceptione){e.printStackTrace();}

returnconn;

}

}

importjava.sql.Connection;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.ArrayList;

publicclassStuDaoimplementsStuDeal{

publicvoidinsert(Stustu)throwsSQLException{

Statementsta=StuFactory.getStatement();

Stringsql="insertintostuvalues("+stu.getId()+","+"'"+stu.getName()+"'"+")";

sta.executeUpdate(sql);

StuFactory.closeStu(sta);

}

publicvoiddelete(intid)throwsSQLException{

Statementsta=StuFactory.getStatement();

sta.executeUpdate("deletefromstuwhereid="+id);

StuFactory.closeStu(sta);

}

publicvoidquery(Stustu)throwsSQLException{

Statementsta=StuFactory.getStatement();

ResultSetrs=sta.executeQuery("select*fromstuwhereid="+stu.getId());

while(rs.next()){

System.out.println("id号为:"+rs.getInt(1)+""+"名字为:"

+rs.getString(2));

}

StuFactory.closeStu(sta,rs);

}

publicArrayListfindCondition(Stustu)throwsSQLException{

ArrayList<Stu>s=newArrayList();

Stringsql="selectid,namefromstuwhere1=1and";

if(stu.getId()!=0){

sql=sql+"id="+stu.getId()+"and";

}

if(stu.getName()!=null){

sql=sql+"name='"+stu.getName()+"'and";

}

if(sql.endsWith("and")){

sql=sql.substring(0,sql.length()-3);

}

Statementsta=StuFactory.getStatement();

ResultSetrs=sta.executeQuery(sql);

while(rs.next()){

Stust=newStu();

st.setId(rs.getInt(1));

st.setName(rs.getString(2));

s.add(st);

}

StuFactory.closeStu(sta,rs);

returns;

}

}

importjava.sql.SQLException;

importjava.util.ArrayList;

publicclassStuTest{

/**

*@paramargs

*@throwsSQLException

*/

publicstaticvoidmain(String[]args)throwsSQLException{

//TODOAuto-generatedmethodstub

/*Stus=newStu();

s.setName("d");

StuDealsdao=newStuDao();

ArrayList<Stu>list=sdao.findCondition(s);

for(inti=0;i<list.size();i++){

System.out.println("学生id为:"+list.get(i).getId()+"学生姓名为:"+list.get(i).getName());

}*/

StuDealsdao=newStuDao();

Stus=newStu(11,"c");

sdao.insert(s);

}

}