Use jsp + jdbc to connect to the database. Use jspjdbc to connect to the database.
This example describes how to connect to a database through jsp + jdbc. Share it with you for your reference. The details are as follows:
The first time I tried JSP + jdbc, I had to follow the example in the book for a long time, that is, I could not connect to the database. So I found out on the Internet that the old jar package is directly incompatible with the new database version. So I got a new database jdbc package and tried it. Here, we share this program with you. The program extracts the user name and password from the webpage logon interface and corresponds to the user name and password in the database to determine whether the program is logged on.
Inc. jsp file:
<%@ page import="java.sql.Connection"%><%@ page import="java.sql.DriverManager"%><%@ page import="java.sql.Statement"%><%@ page import="java.sql.ResultSet"%><%@ page import="java.sql.ResultSetMetaData"%><%String drv = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/demo";String usr = "nari";String pwd = "nari";%>
Welcome. jsp file:
Login_action.jsp file:
<%@ include file="inc.jsp" %><%String username = request.getParameter("username");String password = request.getParameter("password");if(username == null || password == null){ response.sendRedirect("index.jsp");}boolean isValid = false;String sql = "select * from user where username='"+username+"'and password='"+password+"'";out.println("===>"+sql);try{ Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url, usr,pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(rs.next())isValid = true; rs.close(); stm.close(); conn.close();}catch(Exception e){ e.printStackTrace(); out.println(e);}if(isValid){ response.sendRedirect("welcome.jsp");}else response.sendRedirect("index.jsp");%><% /*if(username.endsWith("a"))response.sendRedirect("welcome.jsp");else response.sendRedirect("index.jsp");*/%>
Index. jsp file:
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>
The program is released using tomcat, and myeclipse is edited and debugged.
I hope this article will help you with jsp program design.