JSP操作

    科技2025-04-11  13

    <%-- page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="home.jsp " method="post"> <input type ="text " name="username"> <input type ="text " name="password"> <button type ="submit ">提交</button> </form> </body> </html>

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% //加载数据驱动 Class.forName("com.mysql.jdbc.Driver"); //建立数据库链接 String url = "jdbc:mysql://localhost:3306/book";//数据库链接地址 Connection connection = DriverManager.getConnection(url ,"root","root");//第一个root为数据库的用户名,第二个root为数据库的密码 String sql = "select * from user where username = ? and password = ?";//在PreparedStatement 中使用问好代替实际参数 PreparedStatement ps =connection .prepareStatement(sql); ps.setString(1,request .getParameter("username") ); ps.setString(2,request .getParameter("password") ); //"insert into user(username,password,gender) values (?,?,?)"; ResultSet rs = ps.executeQuery() ; while (rs.next() ){ out.print(rs.getString("username")+"-"+rs.getString(5)+"<br>" ); } /* //使用Statement对象执行sql语句 Statement stmt = connection.createStatement() ; //stmt.executeUpdate()执行增删改操作的函数 String sql = "select * from user"; ResultSet rs = stmt.executeQuery(sql);//执行查询的函数 返回结果集 while (rs.next() ){ out.print(rs.getString("username")+"-"+rs.getString(5)+"<br>" ); } */ %> </body> </html>

    Processed: 0.009, SQL: 8