关于jsp访问mysql数据库的一些问题

时间:2021-11-19 02:32:25

index.jsp <body> <form action="check.jsp" method="post"> 用户名<input type="text" name="uname"><br/> 密码<input type="password" name="upwd"><br/> <input type="submit" value="登录"><br/> </form> </body> check.jsp // 导入驱动,加载具体驱动类 Class.forName("com.mysql.cj.jdbc.Driver"); // 与数据库建立连接 connection = DriverManager.getConnection(URL, USERNAME, PSW); // 发送sql,执行增、删、改 stmt = connection.createStatement(); String name=request.getParameter("uname"); String pwd=request.getParameter("upwd"); //错误写法1://String sql = "select count(*) from user where name=‘ " name " ‘ and pwd=‘ " pwd " ‘ "; String sql = "select count(*) from user where uname=‘ " name " ‘ and upwd=‘ " pwd " ‘ "; // =左边的uname是数据库中定义的字段名,右边的name是上面的String name获取的用户输入的值 //错误写法2://String sql = "select name,pwd from user where name=‘name‘ and pwd=‘pwd‘ ";