1,新建WEB项目:JDBC
2,导入驱动
将mysql-connector-java-5.0.8-bin.jar包放入web-inf目录下面的lib目录中
3,新建User类,放入entity包中
package songyan.login.etity; 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; } }
4,新建dbinfo.properties
选中src,右击new--File
File Name :dbinfo.properties
点击左下角source,写入代码
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day06 user=root password=247418
5.新建Util.java 放入Util包
抽取getConnection()和closeAll(Connection conn,Statement sta,ResultSet rs)
package songyan.login.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 Util { private static String driverClass; private static String url; private static String user; private static String password; static{ ResourceBundle rb= ResourceBundle.getBundle("dbinfo"); driverClass=rb.getString("driverClass"); 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); } public static void closeAll(Connection conn,Statement sta,ResultSet rs) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs=null; } if(sta!=null) { try { sta.close(); } catch (SQLException e) { e.printStackTrace(); } sta=null; } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn=null; } } }
6,新建DoLogin.java
package songyan.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import songyan.login.etity.User; import songyan.login.util.Util; public class Dologin { public static User findUser(String name,String password) throws SQLException { User u=null; Connection conn; PreparedStatement sta; ResultSet rs; String sql="select * from users where name=? and password=?"; conn=Util.getConnection(); sta=conn.prepareStatement(sql); sta.setString(1, name); sta.setString(2, password); rs=sta.executeQuery(); while(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)); } return u; } }
7,新建Login.java
package songyan.servlet; import java.sql.SQLException; import java.util.Scanner; import songyan.login.etity.User; public class Login { public static void main(String[] args) throws SQLException { System.out.println("请输入用户名:"); Scanner in =new Scanner(System.in); String name=in.next(); System.out.println("请输入密码"); String pass=in.next(); User u=Dologin.findUser(name,pass); if(u==null) { System.out.println("用户名或密码不正确"); } else { System.out.println("欢迎你"+name); } } }
8.测试: