j2ee怎么实现进入jsp页面后自动调用方法查询数据库,再将查询结果显示到页面的指定表格中

j2ee怎么实现进入jsp页面后自动调用方法查询数据库,再将查询结果显示到页面的指定表格中,第1张

你是只用jsp技术吗?那么在页面1jsp的body标签处添加onload事件处理函数为init,在事件处理函数init中发送ajax请求,ajax回调函数为getNews,这个ajax请求的url地址为页面2jsp,在2jsp中<%%> 之间写java代码,用jdbc查询数据库,查完了把结果通过jsp内置对象out的print发送出去,这样1jsp的ajax回调函数getNews就可以拿到out的print打印的字符串了,在getNews中使用documentgetElementById得到指定表格,然后把字符串处理后加到这个指定表格里面吧

我刚写了一个只有插入的,望采纳

import javasql;

import javautil;

public class TestPre {

public static void main(String[] args) {

int i=0,deptno=0;//i只做while循环使用,deptno是表dept2中的一个属性,类型是int

String dname=null,loc=null;//dname和loc也是表dept2的属性,类型是String

Scanner s=new Scanner(Systemin);

Systemoutprintln("请输入3个参数");

while(i<3){

try{

deptno=snextInt();

i++;

dname=snext();

i++;

loc=snext();

i++;

}catch(InputMismatchException e){

Systemoutprintln("输入的类型不符,退出");

Systemexit(-1);

}

}

Connection conn=null;

PreparedStatement pstmt=null;

try {

ClassforName("commysqljdbcDriver");

conn = DriverManagergetConnection("jdbc:mysql://localhost/mydata"+ "user=root&password=root");

pstmt=connprepareStatement("insert into dept2 values(,,)");

pstmtsetInt(1, deptno);

pstmtsetString(2, dname);

pstmtsetString(3, loc);

pstmtexecuteUpdate();

Systemoutprintln("插入完成");

} catch (ClassNotFoundException e) {

Systemoutprintln("连接数据库不成功,程序退出");

Systemexit(-1);

} catch (SQLException e) {

Systemoutprintln("连接数据库不成功,程序退出");

Systemexit(-1);

}

finally{

try{

if(pstmt!=null){

pstmtclose();

pstmt=null;

}

if(conn!=null){

connclose();

conn=null;

}

}catch(SQLException e){

eprintStackTrace();

}

}

}

}

boolean execute(String sql)

根据给定的sql串,可能返回多个结果。

如果返回的第一个结果是一个结果集对象(ResultSet),该方法返回true;如果它返回更新影响的行数或没有返回结果,则该方法返回false。

根据你的代码,你的sql串是一个查询语句,虽然没有返回实际记录,但返回类型是结果集对象(ResultSet),故执行execute()方法的返回值依然是true。

以上就是关于j2ee怎么实现进入jsp页面后自动调用方法查询数据库,再将查询结果显示到页面的指定表格中全部的内容,包括:j2ee怎么实现进入jsp页面后自动调用方法查询数据库,再将查询结果显示到页面的指定表格中、编写一个java程序,通过jdbc访问数据库实现对数据库的插入,删除,修改和查询 *** 作、java jdbc 查询oralce数据库,该数据库的表中没记录却返回true等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10200069.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存