目录
1.封装连接数据库语句(放在dao包下方,DBHeper类)
一.什么是JavaBean?
JavaBean泛指的就是Java对象
Java:可以理解为是豆荚 Bean:豆子
二.为什么需要JavaBean?
- 解决代码重复编写,减少代码冗余
- 功能区分明确,避免业务逻辑处理与页面显示处理集中在一起造成混乱
- 提高了代码的维护性
三.如何在JavaWeb中创建Java文件
1.点击mian文件(一般情况下mian文件下方自带一个Java文件),鼠标右键,点击new,点击other,搜索package,新建一个包,在点击package鼠标右键,new---->other--->class,新建一个class。(就和java文件创建的过程一样)
注:如果main下方没有Java文件,可以直接点到src/main/java鼠标右键,新建package,在新建class,就和java建文件一样。
四.完成模糊查询
思路:
1.封装连接数据库语句(放在dao包下方,DBHeper类)
代码如下:
package ulit; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import oracle.jdbc.driver.OracleDriver; public class DBHeper { static{ try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (Exception e) { e.printStackTrace(); } } //定义连接字符创 public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl"; //获得连接 public static Connection getCon() { try { return DriverManager.getConnection(URL,"scott","zking123"); } catch (Exception e) { e.printStackTrace(); } return null; } //关闭资源 public static void getColse(Connection con,PreparedStatement ps,ResultSet rs) { try { if(con!=null&&con.isClosed())con.close(); if(ps!=null)ps.close(); if(rs!=null)rs.close(); } catch (Exception e) { e.printStackTrace(); } } }
2.在pojo包下创建一个新闻实体类
代码如下:
package pojo; import java.io.Serializable; public class News implements Serializable{ private int news_id; private String news_title; private int news_topic; private String news_author; private String news_publisher; private String news_contet; private String news_cover; private int news_count; public int getNews_id() { return news_id; } public void setNews_id(int news_id) { this.news_id = news_id; } public String getNews_title() { return news_title; } public void setNews_title(String news_title) { this.news_title = news_title; } public int getNews_topic() { return news_topic; } public void setNews_topic(int news_topic) { this.news_topic = news_topic; } public String getNews_author() { return news_author; } public void setNews_author(String news_author) { this.news_author = news_author; } public String getNews_publisher() { return news_publisher; } public void setNews_publisher(String news_publisher) { this.news_publisher = news_publisher; } public String getNews_conte