package demo3class; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; public class jdbc { @Test public void textlogin(){ try { login1("zs' or 'zs","zs"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void login(String username,String password) throws ClassNotFoundException, SQLException{ // 1 注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2获取连接 Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/web08","root","root"); //3创建执行SQL的对象 Statement stmt =conn.createStatement(); // 4书写sql语句 String sql="select * from tbl_user where "+"uname='"+username+"'and upassword='"+password+"'"; //5执行Sql语句,获取结果集 ResultSet rs=stmt.executeQuery(sql); //6对结果集进行处理 if(rs.next()){ System.out.println("恭喜你"+username+"登陆成功"); System.out.println(sql); }else{ System.out.println("账号密码错误"); } if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(conn!=null) conn.close(); }