Preliminary JSP+JDBC SQL2008-based login verification program

Source: Internet
Author: User

Simple in the form of code to commemorate, because there is no solution to the problem of SQL2008 drive, and there are a lot of things to learn, so there will be updates ~

The installed software is:sql2008,eclipse,tomcat,jdk, involving the environment configuration, and so on, the installation database feel the deepest, although there are tutorials online, but the actual operation, really feel that this is the real installation software.

Design key:

The key is to validate the page, which must: database connection operation, database record query operation

Submit Interface Code

<%@ page language= "java" pageencoding= "GB2312"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> User Submission Page </title>
<body>
<form action = "ch07_7_yanzheng.jsp" method= "POST" >
User name: <input type= "text" name= "username" >
User password: <input type= "password" name= "pass" ><br></br>
<input type= "Submit" value= "Login" >
</form>>
</body>

Verify the interface code:

<%@ page language= "java" import= "java.sql.*" pageencoding= "GB2312"%>

<title> Login Verification Interface </title>

<% Connection Conn =null;

PreparedStatement pstmt =null;

ResultSet Rs=null;

try{String drivername = "Com.sql.jdbc.Driver";

String dbname= "User";

String url1= "jdbc:sql://localhost/" +dbname;

String url2= "? user=sa&password=lx13784429739";

String url3= "&unicode=true&characterencoding=gb2312";

String Url=url1+url2+url3; Class.forName (drivername);

Conn=drivermanager.getconnection (URL);

Request.setcharacterencoding ("GB2312");

String Name=request.getparameter ("username");

String pw=request.getparameter ("Pass");

String sql= "SELECT * from User_b where (uname=?andupassword=?) ";

pstmt =conn.preparestatement (SQL);

Pstmt.setstring (1,name);

Pstmt.setstring (2,PW);

Rs=pstmt.executequery ();

if (Rs.next ()) {%><%=name%>: Login Successful! <br><%

} else{%>

<%=name%>: Login failed! <br><%}

}catch (Exception e)

{%> An unexpected error occurred! <br><%=e.getmessage ()%> <%}

finally{if (rs!=null) {rs.close ();}

if (pstmt!=null) {pstmt.close ();}

if (conn!=null) {conn.close ();}

}%>

</body>

The database code is too simple to give.

During the writing verification interface, encountered some problems, so in csdn, blog Park, JSP Bar raised a bit of a problem, got enthusiastic netizens help, here to express thanks ~

http://tieba.baidu.com/p/3702849992

http://ask.csdn.net/questions/175550

Problems encountered:

That is, the definition of statement result connection should be placed on the outside of the try or in finally can not be called, in fact, before the question has been aware of the scope of the variable definition of the problem, is not able to solve, this is worth considering.

Problems to be solved:

Problems with database connections

Plan:

Thoroughly understand JSP+JDBC development mode, Learning Javabeen,servlet, the recent JSP, Javabeen, servlet, JDBC various combinations of development.

Preliminary JSP+JDBC SQL2008-based login verification program

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.