1.jsp访问数据库
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
<title>logincheck</title>
</head>
<body>
<%
String NAME="root";
String PWD="密码";
String URL = "jdbc:mysql://localhost:3306/user";
Connection con =null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Class.forName("com.mysql.cj.jdbc.Driver");
con =DriverManager.getConnection(URL,NAME,PWD);
//String sql="insert into users values(?,?)";
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
out.println(name);
String sql="select * from users where name='"+name+"'and password='"+pwd+"'";
pstmt = con.prepareStatement(sql);
// pstmt.setString(1,"zs");
// pstmt.setString(2,"abc123");
rs=pstmt.executeQuery();
if(rs.next()){
out.println("登录成功");
}
else{
out.print("登陆失败");
}
/* while (rs.next()){
String name = rs.getString("name");
String pwd =rs.getString("password");
System.out.println(name+"--"+pwd);
}*/
// int count= pstmt.executeUpdate();
/*if(count!=0){
System.out.println("插入成功");
}
else{
System.out.println("插入失败");
}*/
rs.close();
pstmt.close();
con.close();
%>
</body>
</html>