MVC
来源:互联网 发布:凤凰直播软件下载 编辑:程序博客网 时间:2024/05/17 13:39
Jdbcutil类中代码:
public class JdbcUtils extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String url;
private static String username;
private static String password;
//让驱动只加载一次
static{
try {
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“驱动加载成功”);
} catch (ClassNotFoundException e) {
System.out.println(“驱动加载失败”);
e.printStackTrace();
}
}
public JdbcUtils() {
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
//init方法中的内容会在启动这个servlet时就执行,因为在配置文件中添加了1
@Override
public void init(ServletConfig config) throws ServletException {
//从配置文件中取得传给getConnection()的三个参数,
url=config.getInitParameter(“url”);
username=config.getInitParameter(“username”);
password=config.getInitParameter(“password”);
//System.out.println(getConn());
System.out.println(“我诞生了,哈哈哈哈哈”);
}
public static Connection getConn(){ Connection conn=null; try { conn=DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn;}public static void close(Connection conn,Statement stmt,ResultSet set){ if(set!=null){ try { set.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
}
配置文件web.xml部分代码:
JdbcUtil
/jdbc
JdbcUtil
com.xmt.utils.JdbcUtils
url
jdbc:mysql://localhost:3306/mytest
username
root
password
ROOT
1
StudentServlet
/stuServlet
StudentServlet
com.xmt.contral.StudentServlet
studentDao类中代码:
public class StudentDao {
public List<Student> getStudentList(String sql){ Connection conn=JdbcUtils.getConn(); List<Student> list=new ArrayList<Student>(); Statement stmt=null; ResultSet set=null; try { //得到操作数据库的对象stmt stmt=conn.createStatement(); set=stmt.executeQuery(sql); while(set.next()){ Student student=new Student(); student.setSname(set.getString("Sname")); student.setSage(set.getInt("Sage")); student.setSsex(set.getString("Ssex")); student.setSphone(set.getString("Sphone")); list.add(student); } } catch (SQLException e) { e.printStackTrace(); }finally{ JdbcUtils.close(conn, stmt, set); } return list;}
}
StudentService 中代码:
public class StudentService {
public List StudentList(){
String sql=”select * from student”;
StudentDao sd=new StudentDao();
List list=sd.getStudentList(sql);
return list;
}
}
StudentServlet中代码:
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public StudentServlet() { super(); // TODO Auto-generated constructor stub}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StudentService stuService=new StudentService(); List<Student> list=stuService.StudentList(); //将集合存进request中 request.setAttribute("StuList", list); //实现跳转,并且可以携带数据 request.getRequestDispatcher("index.jsp").forward(request, response);}
}
以上代码基本可以实现从数据库中读取所有数据了,它完成了servlet与数据库间的交互,接下来只要把获取到的数据设置到页面上对应的位置即可。
在index.jsp文件中写Java代码要讲Java代码放在<% java%>中间。
如:<%
for(int i=0;i
- MVC
- mvc
- MVC
- mvc
- MVC
- MVC
- mvc
- MVC
- MVC
- MVC
- MVC
- MVC
- MVC
- MVC
- MVC
- mvc
- MVC
- MVC
- QT下udpsocket一段时间接收不到数据的问题
- 5年时间,研究177位百万富翁,发现这13条规律
- VS2010:出现devenv.sln解决方案保存对话框
- 为什么越没本事的人,?自尊心越强(深度好文) (1)
- PAT甲 1008. Elevator (20)
- MVC
- Java 反射 getClass()
- loadrunner的同步点/集合点
- ListView开源框架
- 模板光谱库的处理
- Scrapy爬取makepolo网站数据深入详解
- PAT甲 1009. Product of Polynomials (25)
- 解决MYSQL中文乱码问题(实测有效)
- **浙大PAT甲级 1010 进制转化