qq_40619811 2022-01-03 19:44 采纳率: 89.8%
浏览 19
已结题

问如何把Java的JDBC代码和index.jsp连接起来

我最近新接触JavaEE。结果我不知道怎么办,贴上代码。
这是Java代码(Connection类)

import java.sql.*;
public class Connection {
public static void connect(String username,String password){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sys","root","ASD123zxc,./");
        String sql="insert into table1 values ('"+username+"','"+password+"');";
        Statement sta=((java.sql.Connection) con).createStatement();
        sta.execute(sql);
        sta.close();
        ((java.sql.Connection) con).close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

这是index.jsp的html代码(我这里只显示body部分的代码)

<table border="0">
 <tr>
    <th>用户名</th>
    <th><input type="text" name="username"></input></th>
    <tr>
    <th>密码</th>
    <th><input type="password",name="password"></input></th>
    </tr>
    <tr>
    <th align="center">
   <button type="submit" name="sure">确定</button>
    </th>
    </tr>
   </table>

我的目的是在用户名和密码框里输入信息,然后点击确定按钮,用户名和密码框里的内容会插入到mysql数据库中,应该怎么做呢?

  • 写回答

2条回答 默认 最新

  • 关注

    到servlet里面查询数据出来,然后用request.setAttribute方法设置变量,页面使用request.getAttribue方法获取,或者用标准标签库获取。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 1月3日

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示