Jsp+jdbc to implement the method of connecting the database _jsp programming

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.