This article illustrates the Jsp+jdbc method for implementing a connection database. Share to everyone for your reference. Specifically as follows:
The first attempt to jsp+jdbc, according to the example of the book toss a half-day, is not even on the database. So looking for materials on the Internet, finally found that the old jar package and the new database is directly incompatible. The new database JDBC package was then tried, and it was done. Here, the program to share with you, the program to achieve the Web page login interface to extract the user name and password, and then the database with the user name password corresponding to determine whether the program by login.
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 ("Userna
Me ");
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 uses Tomcat for publishing, MyEclipse for editing and debugging
I hope this article will help you with the JSP program design.