Use jsp + jdbc to connect to the database. Use jspjdbc to connect to the database.

Source: Internet
Author: User

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.

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.