----------------------------------------------------------------------------------------------------------------------------------------------------------

 泡牛吧!

                                       希望越来越多的光棍能够泡到牛

-----------------------------------------------------------------------------------------------------------------------------------------------------------

共 8篇 前 2 页:    每页5篇 下一页  

javaExam

这是DAO的事例,我没有用jsp进行操作,直接用java运行,代码如下:
DAOFactory 抽象的DAOFactory类
MysqlDAOFactory mysql数据库数据对象工厂、mysql的具体DAOFactory实现
MysqlExamDAO Exam的Mysql的实现
ExamDAO Exam的基本DAO接口
Exam Exam值的对象
exambrowse 使用的DAO和DAO工厂的客户端
haohao   2005-01-04 20:52:47 阅读:1040  评论:0  引用:0

/*
* MysqlExamDAO.java
*
* Created on 2005年1月4日, 下午2:19
*/


package exam;
import java.util.Collection;
import java.util.*;
import java.sql.*;
import javax.sql.*;
/**
*
* @author liuyp
*/

public class MysqlExamDAO implements ExamDAO {
public static Statement stmt=null;
public static PreparedStatement pst=null;
public static ResultSet rs=null;
Exam em=new Exam();
public MysqlExamDAO() {
}
public int insertExam(Exam e)
{
try{
pst=MysqlDAOFactory.PreparedStatement(
"insert into exam(examid,name,msg) values(?,?,?)");
pst.setInt(1,e.getExamID());
pst.setString(2,e.getName());
pst.setString(3,e.getMsg());
int sum=pst.executeUpdate();
return sum;
}
catch(SQLException ex){
System.err.println(ex.getMessage());
return 0;
}
}
public boolean deleteExam(int examid)
{
try{
stmt=MysqlDAOFactory.createStatement();
int sum=stmt.executeUpdate(
"delete from where examid="+examid+"");
if(sum<0) return false;
else return true;
}
catch(SQLException ex){
System.err.println(ex.getMessage());
return false;
}
}
public Exam findExam(String name)
{
stmt=MysqlDAOFactory.createStatement();
rs=stmt.executeQuery(
"select * from exam where name="+name+");
if(rs.next()){
em.setExamID(rs.getString(1));
em.setName(rs.getString(2));
em.setSize(rs.getString(3));
return em;
}
else return null;

}
public boolean updateExam(Exam em)
{

try{
pst=MysqlDAOFactory.PreparedStatement(
"update exam set name=?,msg=? where id =?);
pst.setInt(1,em.getExamID());
pst.setString(2,em.getName());
pst.setString(3,em.getMsg());
int sum=pst.executeUpdate();
return true;
}
catch(SQLException ex){
System.err.println(ex.getMessage());
return false;
};
}
public Collection selectExam()
{
stmt=MysqlDAOFactory.createStatement();
rs=stmt.executeQuery(
"selct * from exam");
List list = new ArrayList();

while(rs.next())
{
pst.setInt(1,em.getExamID());
pst.setString(2,em.getName());
pst.setString(3,em.getMsg());
list.add(em);
}
if(list.size() == 0) return null;
else return list;

}
}
haohao   2005-01-04 19:59:00 阅读:1064  评论:0  引用:0

/*
* exambrowse.java
*
* Created on 2005年1月4日, 下午3:48
*/


package exam;

/**
*
* @author haohao*/

public class exambrowse {

/** Creates a new instance of exambrowse */
public exambrowse() {
}
public static void main(String args[])
{
Exam em=new Exam();
em.setExamID(1);
em.setName(
"test");
em.setMsg(
"test");
DAOFactory mysql=DAOFactory.getDAOFactory(1);
ExamDAO examDAO=mysql.getExamDAO();
int sum=examDAO.insertExam(em);
Exam m= examDAO.findExamID(1);
m.setName(
"haohao");
m.setMsg(
"haohao");
examDAO.updateExam(m);
Collection list=examDAO.selectExam();
Exam []am=new Exam[list.size()];
am=(Exam[])list.toArray(am)
}

}

haohao   2005-01-04 20:46:21 阅读:977  评论:0  引用:0

package exam;
import java.sql.SQLException;
public abstract class DAOFactory {
public static final int MYSQL=1;
public static final int ORACLE=2;
public abstract ExamDAO getExamDAO();
public static DAOFactory getDAOFactory(int whichFactory)
{
switch(whichFactory)
{
case MYSQL:
return new MysqlDAOFactory();
default :
return null;
}
}
}
haohao   2005-01-04 20:02:40 阅读:1145  评论:0  引用:0

/*
* Exam.java
*
* Created on 2005年1月4日, 下午1:45
*/


package exam;


import java.io.Serializable;

/**
* @author haohao
*/

public class Exam implements Serializable {
String examid;
String name;
String msg;
public void setExamID(String examid)
{
this.examid=examid;
}
public void setName(String name)
{
this.name=name;

}
public void setMsg(String msg)
{
this.msg=msg;
}
public String getExamID()
{
return examid;
}
public String getName()
{
return name;
}
public String getMsg()
{
return Msg;
}
}

haohao   2005-01-04 20:02:10 阅读:996  评论:0  引用:0

/*
* ExamDAO.java
*
* Created on 2005年1月4日, 下午1:54
*/


package exam;

/**
*
* @author liuyp
*/

import java.util.*;
import java.util.Collection;

public interface ExamDAO {
public int insertExam(Exam e);
public boolean deleteExam(Exam ee);
public Exam findExam();
public boolean updateExam();
public Collection selectExam();
}


haohao   2005-01-04 20:01:37 阅读:988  评论:0  引用:0
/*
* MysqlDAOFactory.java
*
* Created on 2005年1月4日, 下午2:17
*/


package exam;
import java.sql.*;
import javax.sql.*;
import java.sql.Connection;
public class MysqlDAOFactory extends DAOFactory
{

public static final String DRIVER=
"";
public static final String URL=
"jdbc:mysql://localhost:3306/joke?user=root";
public static final String USRNAMW=
"";
public static final String PAS=
"";
public static Connection conn = null;
public static Statement stmt=null;
public static PreparedStatement pst=null;
public MysqlDAOFactory()
{
try {
Class.forName(
"com.mysql.jdbc.Driver");
}
catch (java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
public static Statement createStatement()
{
try{
conn = DriverManager.getConnection(URL);
stmt=conn.createStatement();
return stmt;

}
catch(SQLException ex){
System.err.println(ex.getMessage());
return null;
}
}
public static PreparedStatement PreparedStatement(String sql)
{
try{
conn = DriverManager.getConnection(URL);
pst = conn.prepareStatement(sql);
// System.out.println("ok");
return pst;
}
catch(SQLException ex){
System.err.println(ex.getMessage());
return null;
}
}

public ExamDAO getExamDAO()
{
return new MysqlExamDAO();
}
}
haohao   2005-01-04 19:58:19 阅读:982  评论:0  引用:0
create table exam
examid int,
name varchar(20),
msg varchar(30),
primary key(examid).
haohao   2005-01-04 19:46:53 阅读:890  评论:0  引用:0

一切版权属于个人(转载例外)