User.java (封装数据) -------------> Dologin.java
属性和User表字段相等
Login.java
输入用户名,输入密码 ------------->Dologin.java
DButils (工具类)
getConnection();closeAll(); ------------->Dologin.java
Dologin.java
1根据用户密码查询User对象
User u =FindUser(); //拼接成sql语句查询数据库 ------JDBC getConnection
//和数据库交互 查到的话封装到User对象里去,就不是null
2判断(user对象!=null)
登录成功!
否则:
用户名错误
Login.java 客户端 climent
package com.itheima.client;
import java.util.Scanner;
import com.itheima.entity.User;
import com.itheima.service.DoLogin;
public class Login {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = input.nextLine();
System.out.println("请输入密码:");
String pwd = input.nextLine();
DoLogin dl = new DoLogin(); //service后台服务
User user = dl.findUser(name, pwd);//调用查询用户的方法
if(user!=null){
System.out.println("欢迎你:"+user.getName());
}else{
System.out.println("用户名或密码错误!");
}
}
}
package com.itheima.entity; USER.java (封装数据)
import java.util.Date;
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password
+ ", email=" + email + ", birthday=" + birthday + "]";
}
}
package com.itheima.service; Dologin.java 最最主要的操作都在这
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.itheima.entity.User;
import com.itheima.util.DBUtils;
public class DoLogin {
/**
* 根据用户名和密码查询用户对象信息
* @param name
* @param pwd
* @return u
*/
public User findUser(String name,String pwd){
Connection conn = null; //这边需要ctrl shift o
PreparedStatement stmt = null; //preparedStatement
//***原来是Statememnt //preparedStatemrnt和prepareStatemnt
ResultSet rs = null;
User u = null;
try {
conn = DBUtils.getConnection(); //工具类得到连接对象Connection
String sql ="SELECT * FROM users WHERE NAME=? AND PASSWORD=?";
stmt = conn.prepareStatement(sql); //prepare
//得到执行sql语句的对象Statement
//原来是createStatement,createStatemrnt在sql上面
//现在这个叫做预编译
//*****(老版本)stmt.executeQuery("select*from users WHERE NAME='"+name+"'AND PASSWORD ='"+pwd+"'")
//*****(老版本) 密码输入为 asdf' or '1'='1
//给?赋值
stmt.setString(1, name); //第一个问号
stmt.setString(2, pwd); //有几个问号就有几个set
rs = stmt.executeQuery();//执行sql语句
if(rs.next()){
u = new User();
u.setId(rs.getInt(1));
u.setName(rs.getString(2));
u.setPassword(rs.getString(3));
u.setEmail(rs.getString(4));
u.setBirthday(rs.getDate(5));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtils.closeAll(rs, stmt, conn);//不管发生任何问题,都要
}
return u;
}
}
或者是@test //这个选自test 可能和该内容无关
public void testInsert(){
Connection conn = null; //
PreparedStatement stmt = null; //****去掉rs//去掉rs和list
try {
conn = DBUtils.getConnection();
stmt = conn.prepareStatement("INSERT INTO users VALUES(?,?,?,?,?)");
stmt.setInt(1, 5);
stmt.setString(2, "tom");
stmt.setString(3, "333");
stmt.setString(4, "tom@163.com");
//stmt.setDate(5, new java.sql.Date(System.currentTimeMillis()));
stmt.setString(5, "2015-09-11");
int i = stmt.executeUpdate();
if(i>0){
System.out.println("success");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtils.closeAll(null, stmt, conn); //****原来rs的位置,给一个null值
} //等于null就不管他了
}
Dbinfo.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///day06
user=root
password=abc
DBUtils.java (封装jdbc)
package com.itheima.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtils {
private static String driverClass; //create local variable "USER"
private static String url;
private static String user;
private static String password;
static{ //静态代码块----用来赋值
//此对象是用于加载properties文件数据的//ResourceBundle操作属性资源文件的
ResourceBundle rb = ResourceBundle.getBundle("dbinfo");
//创建一个dbinfo.properties
//给上面4个变量赋值
driverClass = rb.getString("driverClass"); //这个是驱动driver 此时+’’
url = rb.getString("url");
user = rb.getString("user");
password = rb.getString("password");
try {
Class.forName(driverClass); //不+’’
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//得到连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);//右键 create local variable
}
//关闭资源 //ctrl shift o
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
//***如果你这个之前执行rs语句出错,那么就没有开启,不需要关闭
try {
rs.close(); //close的时候也有异常
} catch (SQLException e) {
e.printStackTrace();
}
rs = null; //用来清空数据
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}