jsp中怎么使用数据库
2个回答
2015-12-14 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
JSP使用数据库的例子:
showByJdbcOdbc.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML><BODY bgcolor=cyan>
<% Connection con;
Statement sql;
ResultSet rs;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{ out.print(e);
}
try { con=DriverManager.getConnection("jdbc:odbc:mymoon","sa","sa");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM employee WHERE salary>3000");
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>"+"雇员号");
out.print("<th width=100>"+"姓名");
out.print("<th width=50>"+"出生日期");
out.print("<th width=50>"+"薪水");
out.print("</TR>");
while(rs.next())
{ out.print("<tr>");
out.print("<td >"+rs.getString(1)+"</td>");
out.print("<td >"+rs.getString(2)+"</td>");
out.print("<td >"+rs.getDate("birthday")+"</td>");
out.print("<td >"+rs.getFloat("salary")+"</td>");
out.print("</tr>") ;
}
out.print("</table>");
con.close();
}
catch(SQLException e)
{ out.print(e);
}
%>
</BODY></HTML>
showByJdbcOdbc.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML><BODY bgcolor=cyan>
<% Connection con;
Statement sql;
ResultSet rs;
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{ out.print(e);
}
try { con=DriverManager.getConnection("jdbc:odbc:mymoon","sa","sa");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM employee WHERE salary>3000");
out.print("<table border=2>");
out.print("<tr>");
out.print("<th width=100>"+"雇员号");
out.print("<th width=100>"+"姓名");
out.print("<th width=50>"+"出生日期");
out.print("<th width=50>"+"薪水");
out.print("</TR>");
while(rs.next())
{ out.print("<tr>");
out.print("<td >"+rs.getString(1)+"</td>");
out.print("<td >"+rs.getString(2)+"</td>");
out.print("<td >"+rs.getDate("birthday")+"</td>");
out.print("<td >"+rs.getFloat("salary")+"</td>");
out.print("</tr>") ;
}
out.print("</table>");
con.close();
}
catch(SQLException e)
{ out.print(e);
}
%>
</BODY></HTML>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询