下面是类DAO类,命名为NewsRealeseDao.java,对于DAO类,主要用于实现数据库的连接问题,进行数据库的链接,在连接过程中需要用到各种有关数据库的技术,在这里我们使用的是Oracle数据库,其获取链接的方式为如下代码,其中第一行为驱动程序名,第二行为数据库地址(orcl为Oracle数据库中的SID,在安装Oracle数据库时需要记住),第三行为具体的连接,及使用URL找到数据库位置,然后使用密码wjk139登录到Scott账户
//获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139");
其他的操作分别为增删改查的具体代码,分别对应Servlet中或jsp中需要实例化的代码,下面就是具体的DAO类代码:
package dao; import com.lut.beans.NewsRealese; import static java.lang.System.out; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class NewsRealeseDao { public ArrayList queryAllNews() throws Exception { Connection conn = null; ArrayList newsRealese = new ArrayList(); try { //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139"); //运行SQL语句 String sql = "select * from newmessage";//获取 Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { //实例化VO NewsRealese news = new NewsRealese(); news.setNewsId(rs.getString("newsid")); news.setClassId(rs.getString("CLASSID")); news.setKindId(rs.getString("KINDID")); news.setMyOther(rs.getString("MYOTHER")); news.setHeadTitle(rs.getString("HEADTITLE")); news.setContent(rs.getString("CONTENT")); news.setConnectRealtive(rs.getString("CONNECTREALTIVE")); news.setAuthor(rs.getString("AUTHOR")); news.setEditor(rs.getString("EDITOR")); news.setNewsFrom(rs.getString("NEWSFROM")); news.setTop(rs.getString("TOP")); news.setNewsTime(rs.getString("NEWSTIME")); news.setHits(rs.getString("HITS")); news.setState(rs.getString("STATE")); news.setTag(rs.getString("TAG")); newsRealese.add(news); } rs.close(); stat.close(); } catch (Exception e1) { e1.printStackTrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (Exception ex) { } return newsRealese; } } //查询一个消息 public ArrayList queryOneNews(int newsid) throws Exception { Connection conn = null; ArrayList newsRealese = new ArrayList(); int temp_id = newsid; try { //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139");//不安全 //运行SQL语句 Statement stat = conn.createStatement(); String sql = "select * from newmessage where newsid=?";//获取newsid,使用?代替字符串,以免会发生错误 PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, Integer.valueOf(newsid)); ResultSet rs = ps.executeQuery();// 之前已经给了sql字符串,所以executeUpdate是无参的。 while (rs.next()) { //实例化VO NewsRealese news = new NewsRealese(); news.setNewsId(rs.getString("newsid")); news.setClassId(rs.getString("CLASSID")); news.setKindId(rs.getString("KINDID")); news.setMyOther(rs.getString("MYOTHER")); news.setHeadTitle(rs.getString("HEADTITLE")); news.setContent(rs.getString("CONTENT")); news.setConnectRealtive(rs.getString("CONNECTREALTIVE")); news.setAuthor(rs.getString("AUTHOR")); news.setEditor(rs.getString("EDITOR")); news.setNewsFrom(rs.getString("NEWSFROM")); news.setTop(rs.getString("TOP")); news.setNewsTime(rs.getString("NEWSTIME")); news.setHits(rs.getString("HITS")); news.setState(rs.getString("STATE")); news.setTag(rs.getString("TAG")); newsRealese.add(news); } rs.close(); stat.close(); } catch (Exception e1) { e1.printStackTrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (Exception ex) { } return newsRealese; } } //删除数据 public String deleteOneNews(int newsid) throws Exception { Connection conn = null; ArrayList newsRealese = new ArrayList(); int temp_id = newsid; try { //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139");//不安全 //运行SQL语句 Statement stat = conn.createStatement(); String sql = "DELETE FROM newmessage WHERE newsid =?";//获取newsid,使用?代替字符串,以免会发生错误 PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, Integer.valueOf(newsid)); ResultSet rs = ps.executeQuery();// 之前已经给了sql字符串,所以executeUpdate是无参的。 rs.close(); stat.close(); } catch (Exception e1) { e1.printStackTrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (Exception ex) { } return newsRealese.toString(); } } //插入数据 public String insertOneNews(ArrayList addnews_list) throws Exception { Connection conn = null; /* for(int i=0,j=1;i<addnews_list.size();i++,j++) { System.out.println("j:"+j+"值:"+addnews_list.get(i).toString()); }*/ try { //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139");//不安全 //运行SQL语句 Statement stat = conn.createStatement(); String sql = "insert into newmessage(newsId,classId,kindId,myOther,headTitle,content,connectRealtive,author,editor,newsFrom" + ",top,newsTime,hits,state,tag) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";//获取newsid,使用?代替字符串,以免会发生错误 PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, Integer.valueOf(addnews_list.get(0).toString())); ps.setInt(2, Integer.valueOf(addnews_list.get(1).toString())); ps.setInt(3, Integer.valueOf(addnews_list.get(2).toString())); ps.setInt(4, Integer.valueOf(addnews_list.get(3).toString())); ps.setString(5, addnews_list.get(4).toString()); ps.setString(6, addnews_list.get(5).toString()); ps.setString(7, addnews_list.get(6).toString()); ps.setString(8, addnews_list.get(7).toString()); ps.setString(9, addnews_list.get(8).toString()); ps.setString(10, addnews_list.get(9).toString()); ps.setInt(11, Integer.valueOf(addnews_list.get(10).toString())); ps.setString(12, addnews_list.get(11).toString()); ps.setString(13, addnews_list.get(12).toString()); ps.setString(14, addnews_list.get(13).toString()); ps.setString(15, addnews_list.get(14).toString()); for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) { System.out.println("j:" + j + "值:" + addnews_list.get(i).toString()); } int i = ps.executeUpdate(); conn.commit(); System.out.println("成功添加" + i + "行"); stat.close(); conn.close(); return i + "conn:" + conn; } catch (Exception e1) { e1.printStackTrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (Exception ex) { } } return conn.toString(); } //更新数据 public String updateOneNews(ArrayList addnews_list) throws Exception { Connection conn = null; /* for(int i=0,j=1;i<addnews_list.size();i++,j++) { System.out.println("j:"+j+"值:"+addnews_list.get(i).toString()); }*/ try { //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139");//不安全 //运行SQL语句 Statement stat = conn.createStatement(); //获取newsid,使用?代替字符串,以免会发生错误 String sql = "UPDATE newmessage set classId=?,kindId=?,myOther=?,headTitle=?,content=?," + "connectRealtive=?,author=?,editor=?,newsFrom=?,top=?,newsTime=?,hits=?,state=?,tag=? where newsid=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, Integer.valueOf(addnews_list.get(1).toString())); ps.setInt(2, Integer.valueOf(addnews_list.get(2).toString())); ps.setInt(3, Integer.valueOf(addnews_list.get(3).toString())); ps.setString(4, addnews_list.get(4).toString()); ps.setString(5, addnews_list.get(5).toString()); ps.setString(6, addnews_list.get(6).toString()); ps.setString(7, addnews_list.get(7).toString()); ps.setString(8, addnews_list.get(8).toString()); ps.setString(9, addnews_list.get(9).toString()); ps.setInt(10, Integer.valueOf(addnews_list.get(10).toString())); ps.setString(11, addnews_list.get(11).toString()); ps.setString(12, addnews_list.get(12).toString()); ps.setString(13, addnews_list.get(13).toString()); ps.setString(14, addnews_list.get(14).toString()); ps.setInt(15, Integer.valueOf(addnews_list.get(0).toString())); for (int i = 0, j = 1; i < addnews_list.size(); i++, j++) { System.out.println("j:" + j + "值:" + addnews_list.get(i).toString()); } int i = ps.executeUpdate(); conn.commit(); System.out.println("成功更新" + i + "行"); stat.close(); conn.close(); return i + "conn:" + conn; } catch (Exception e1) { e1.printStackTrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (Exception ex) { } } return conn.toString(); } public String ischecked(String user, String pass) throws Exception { Connection conn = null; //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; try { conn = DriverManager.getConnection(url,user, pass);//不安全 //建立连接 return conn.toString(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } return null; } }