2020.10.7课堂程序

    科技2023-12-28  93

    home.jsp <% //加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); //建立数据库连接 String url="jdbc:mysql://localhost:3306/book";//数据库连接地址 Connection connection= DriverManager.getConnection(url,"root","root"); //第一个root为数据库的用户名 第二个root为数据库的密码 //使用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(3)+"<br>"); } %> 运行结果 使用PreparedStatement执行sql语句 String sql="insert into user(username,password,gender)values(?,?,?)"; ResultSet rs=ps.executeQuery(); while (rs.next()){ out.print(rs.getString("username")+"-"+rs.getString(3)+"<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(3)+"<br>"); } */ %> index文件 <form action="home.jsp" method="post"> <input type="text" name="username"> <input type="text" name="password"> <button type="submit">提交</button> 运行结果
    Processed: 0.018, SQL: 8