jdbc连接数据库

JDBC是一种用于执行SQL语句的API,由一组类与接口组成,通过调用这些类和接口所提供的方法,可以使用标准的SQL语言来存取数据库中的数据。

String driverName = "com.mysql.jdbc.Driver";
    String dbName = "students";
    String url1 = "jdbc:mysql://localhost:3306/"+dbName;
    String url3 = "&useUnicode = true & characterEncoding = UTF-8";
    try{
    Class.forName(driverName);
    Connection conn = DriverManager.getConnection(url1,"root","123456");
    String sql = "select pwd from user where ID = ?";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String ID = request.getParameter("ID");
    pstmt.setString(1,ID);
    ResultSet rs = pstmt.executeQuery();
    String pwd = "";
    while(rs.next()) 
    pwd = rs.getString("pwd");
    if(pwd.equals("")) {out.print("用户不存在");}
    else {
    String Dpwd = request.getParameter("ipwd");
    rs.close();
    pstmt.close();
    conn.close();
    if(pwd.equals(Dpwd)){ %>
        <jsp:forward page = "access.jsp"></jsp:forward>    
    <% }else {out.print("密码错误");%>
    <%}}%>
       <a href = "login.jsp">返回登陆</a>
     <%}catch(SQLException e) {
    e.printStackTrace();
    }%

 

posted on 2017-10-20 21:43  暗影蔽日  阅读(98)  评论(0编辑  收藏  举报

导航