Introduction to Software engineering 01:javaweb connect SQL Server database and complete a login interface and its functional design.

Source: Internet
Author: User

1. Connect to the database
Package com.jaovo.msg.Util;
Import java.sql.*;

public class Dbutil {
public static Connection getconnection () {
Load Driver
String dbdrive= "Com.mysql.jdbc.Driver";
try {
try {
Class.forName (dbdrive). newinstance ();
} catch (Instantiationexception e) {

E.printstacktrace ();
} catch (Illegalaccessexception e) {

E.printstacktrace ();
}
} catch (ClassNotFoundException e) {

E.printstacktrace ();
}
Connecting to a database
Connection Connection=null;
String url= "Jdbc:mysql://localhost:3307/l_user";
try {
Connection=drivermanager.getconnection (URL, "root", "root");
} catch (SQLException e) {

E.printstacktrace ();
}
return connection;
}
public static void Close (Connection Connection) {
try {
if (connection! = null) {
Connection.close ();
}

} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Close (PreparedStatement preparedstatement) {
try {
if (PreparedStatement! = null) {
Preparedstatement.close ();
}

} catch (SQLException e) {

E.printstacktrace ();
}
}
public static void Close (ResultSet ResultSet) {
try {
if (ResultSet! = null) {
Resultset.close ();
}

} catch (SQLException e) {

E.printstacktrace ();
}
}

}

Summary: Connect to a database step
1. Import java.sql Package
Import java.sql.*;
2. Load the database's connection address, user name and password.
Class.forName ("Com.mysql.jdbc.Driver");
3. Define the database's link address, user name and password.
String user = "root";
String password = "root";
String url = "Jdbc:mysql://localhost:3307/jaovo_msg";

4. Get the Connection object to the database.
Connection = Drivermanager.getconnection (Url,user,password);
5. Declare the SQL statement.
String sql = "SELECT * from User1";
6. Get the Statement object.
PreparedStatement = connection.preparestatement (sql);
7. Execute the SQL statement.
ResultSet = Preparedstatement.executequery ();
8. Process the return statement of the SQL statement.
while (Resultset.next ()) {
user = new User ();
User.setname (resultset.getstring ("user"));
User.setpassword (resultset.getstring ("password"));
Users.add (user);
}
9. Close the object
Dbutil.close (ResultSet);
Dbutil.close (PreparedStatement);
Dbutil.close (connection);
3. Login User Class
Package Com.jaovo.msg.model;

public class User {
private String name;
private String password;
Public String GetName () {
return name;
}
Public String GetPassword () {
return password;
}
public void SetName (String name) {
This.name=name;
}
public void SetPassword (String password) {
This.password=password;
}

}

4. Interface of program function method
Package Com.jaovo.msg.dao;

Import java.util.List;

Import Com.jaovo.msg.model.User;

Public interface Iuserdao {
Public list<user> load ();
Public User Load (String name);

}

5. Classes that implement functional methods

Package Com.jaovo.msg.dao;
Import Com.jaovo.msg.Util.DBUtil;
Import Com.jaovo.msg.model.User;
Import java.sql.*;
Import java.util.ArrayList;
Import java.util.List;
public class Userdaoimpl implements iuserdao{
@Override
Public User Load (String name) {

Connection Connection = Dbutil.getconnection ();
Preparing SQL statements
String sql = "SELECT * from user1 where user = '" +name+ "'";
To create a statement transfer object
PreparedStatement preparedstatement = null;
ResultSet ResultSet = null;
Only user objects can be placed in the collection
User user = null;
try {
PreparedStatement = connection.preparestatement (sql);
ResultSet = Preparedstatement.executequery ();
if (Resultset.next ()) {
user = new User ();
User.setpassword (resultset.getstring ("password"));
}
} catch (SQLException e) {

E.printstacktrace ();
}finally {
Close Resource
Dbutil.close (ResultSet);
Dbutil.close (PreparedStatement);
Dbutil.close (connection);
}
return user;
}
@Override
Public list<user> load () {
Connection Connection = Dbutil.getconnection ();
Preparing SQL statements
String sql = "SELECT * from User1";
To create a statement transfer object
PreparedStatement preparedstatement = null;
ResultSet ResultSet = null;
Only user objects can be placed in the collection
list<user> users = new arraylist<user> ();
User user = null;

try {
PreparedStatement = connection.preparestatement (sql);
ResultSet = Preparedstatement.executequery ();
while (Resultset.next ()) {
user = new User ();
User.setname (resultset.getstring ("user"));
User.setpassword (resultset.getstring ("password"));
Users.add (user);
}
} catch (SQLException e) {

E.printstacktrace ();
}finally {
Close Resource
Dbutil.close (ResultSet);
Dbutil.close (PreparedStatement);
Dbutil.close (connection);
}

return users;
}
}


6. User Login Interface
<!--login.jsp--
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! 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 Login </title>
<body>
<form action= "dologin.jsp" method= "Get" >
<table align= "center" width= "30%" border= "1" >
<tr><td> Login name:</td> s
<td><input type= "text" name= "username"/></td></tr>
<tr><td> Login Password:</td>
<td><input type= "password" name= "password"/></td></tr>
&LT;TR&GT;&LT;TD colspan= "2" align= "center" ><input type= "submit" value= "Login" ><td></tr>
</table>
</form>
</body>

7. Implement user Login.
<% @page import= "Com.jaovo.msg.dao.UserDaoImpl"%>
<% @page import= "Com.jaovo.msg.model.User"%>
<% @page import= "Java.util.List"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<%
The parameters that the receiving client passed over
String username = request.getparameter ("username");
String Password = request.getparameter ("password");
Userdaoimpl a=new Userdaoimpl ();

User user=a.load (username);
String Password1=user.getpassword ();//The password passed by the login window is more than the password queried in the database
if (Password.equals (Password1)) {
OUT.PRINTLN ("Login Successful");
}
Else
OUT.PRINTLN ("Input password or login name is wrong!") ");
%>

Introduction to Software engineering 01:javaweb connect SQL Server database and complete a login interface and its functional 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.