1.连接数据库
package com.jaovo.msg.Util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() {
//加载驱动
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();
}
//连接数据库
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();
}
}
}
总结:连接数据库步骤
1.导入java.sql包
import java.sql.*;
2.加载数据库的连接地址,用户名和密码。
Class.forName("com.mysql.jdbc.Driver") ;
3.定义数据库的链接地址,用户名和密码。
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3307/jaovo_msg";
4.得到与数据库的连接对象。
connection = DriverManager.getConnection(url,user,password);
5.声明sql语句。
String sql = "select * from user1 ";
6.得到语句对象。
preparedStatement = connection.prepareStatement(sql);
7.执行Sql语句。
resultSet = preparedStatement.executeQuery();
8.处理Sql语句的返回语句。
while(resultSet.next()) {
user = new User();
user.setName(resultSet.getString("user"));
user.setPassword(resultSet.getString("password"));
users.add(user);
}
9.关闭对象
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
3.登录用户类
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.程序功能方法的接口
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.实现功能方法的类
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();
//准备sql语句
String sql = "select * from user1 where user =‘"+name+"‘";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
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 {
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return user;
}
@Override
public List<User> load() {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from user1 ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
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 {
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return users;
}
}
6.用户登录界面
<!-- 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="doLogin.jsp" method="get" >
<h6>用户登录</h6>
<table align="center" width="30%" border="1">
<tr><td> 登录名:</td> s
<td><input type=" text" name="username"/></td></tr>
<tr><td> 登录密码:</td>
<td><input type="password" name="password"/></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="登录" ><td></tr>
</table>
</form>
</body>
</html>
7.实现用户登录。
<%@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">
<html>
<%
//接收客户端传递过来的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDaoImpl a=new UserDaoImpl();
User user=a.load(username);
String password1=user.getPassword();//登录窗口传递过来的密码与数据库中查询到的密码相比较
if(password.equals(password1)){
out.println("登录成功");
}
else
out.println("输入密码或登录名有误!");
%>
</html>