MVC jsp+servlet+javabean 连接Mysql数据库測试demo
本文介绍的是怎样使用MVC架构去实现jsp+servlet+javabean连接数据库
首先我们应该了解什么是MVC:
MVC包含三个部分 :
①View:由各种JSP页面组成。
②Controller:由各种Servlet组成,Controller就是将View和Model来进行匹配,用什么Model实现,调用哪个View来显示。
③Model:由各种javabean组成,也是MVC的核心,
以下就是简单的用MVC架构去实现一个JSP(View)+Servlet(Controller)+javabean(Model)实现MySql查询功能的Demo。
安装好例如以下软件:
1.MySQL
2.MyEclipse
3.下载好MySQL 的JDBC 的连接驱动jar url:http://pan.baidu.com/s/1sjBCfZR
一、创建数据库。
1.我们使用MySQL中的test数据库来创建须要測试的表:
在test数据库下创建名字为testTable的表,设置ID为主键,name等属性。点击apply,至此就完毕了数据库的创建。然后给定一些值例如以下4所看到的:
二、创建Web Project
1.打开MyEclipse -> File-> new -> Web Project 创建完后在src下创建包beans和ServletDB最后得到例如以下的文件结构:
2.编写beans
在beans包下创建两个javabeans分别为DAL.java 和 DB.java
DB.java 注意MySqlusername和password是你本机MySql的 username和password与下面的代码可能不一样须要改动。
package beans; import java.sql.*; public class DB { static String driver = "com.mysql.jdbc.Driver"; static String url = "jdbc:mysql://127.0.0.1:3306/test"; static String user = "root";//连接时的username static String password = "root";//连接时的密码 static Connection aConnection; public static Connection connectDB(){ try{ Class.forName(driver); aConnection = DriverManager.getConnection(url,user,password); if(aConnection.isClosed()) { System.out.print("Succeeded!"); } }catch(ClassNotFoundException e){ System.out.println(e); } catch (SQLException e) { e.printStackTrace(); } return aConnection; } public static ResultSet executeQuery(String sqlx) throws SQLException{ connectDB(); ResultSet rs = null; Statement stmt = aConnection.createStatement(); try { rs = stmt.executeQuery(sqlx); } catch (SQLException e) { e.printStackTrace(); } return rs; } }
package beans; import java.sql.ResultSet; import java.sql.SQLException; public class DAL { public static String sqlx=""; public static String testQueryByID(String ID){ ResultSet rs = null; String result = ""; sqlx = "SELECT * FROM testtable where ID = '"+ID+"'"; try { rs = DB.executeQuery(sqlx); } catch (SQLException e) { e.printStackTrace(); } try { while(rs.next()){ result = result +"ID:"+rs.getString("ID")+" Name:"+rs.getString("name")+"</br>"; } } catch (SQLException e) { e.printStackTrace(); } return result; } }
3.编写servlet
File-> new -> servlet->确定包名为servletDB然后取名为test即可了
重要的是在本test方法中增加调用beans方法的语句。
test.java
package servletDB; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import beans.DAL; public class test extends HttpServlet { /** * Constructor of the object. */ public test() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String ID = request.getParameter("ID");//获取传过来的ID String result = ""; result = DAL.testQueryByID(ID);//调用DAL中的testQueryByID方法而且将get过来的ID传过去 PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(result); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void init() throws ServletException { // Put your code here } }
4.配置Servlet
进入WebRoot-〉Web-INF -〉web.xml加入例如以下代码
<servlet-name>test</servlet-name> <servlet-class>servletDB.test</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/servletDB/test</url-pattern>
5.加入MySQL的Driver的jar支持
将下载好的mysql-connector-java-5.1.30-bin.jar 放在好找的地方
右击项目->Properties->Java Build Path->Libraries->Add External 选择下载好的jar即可了
6.写index.jsp
将body中的代码替换为例如以下代码
<form action="servletDB/test" method="get"> 请输入用户ID:<input type="text" name="ID"><br> <input type="submit" value="提交"> </form>
三、測试
1.点击Run
2.输入測试ID 1
成功的获取到了test数据库testtable中ID为1的数据。