当前位置:Gxlcms > 数据库问题 > JSP直接连接sql2008数据库并显示

JSP直接连接sql2008数据库并显示

时间:2021-07-01 10:21:17 帮助过:5人阅读

<%@ page contentType="text/html; charset=utf-8" language="java" errorPage=""%> 2 <%@ page import="java.sql.*" %> 3 <html> 4 <head> 5 <title>欢迎</title> 6 </head> 7 <%! 8 public int count=0; 9 public String info() 10 { 11 return "hello"; 12 } 13 %> 14 <body> 15 <% 16 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") ; 17 Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=QHY","sa","sa"); 18 Statement stmt = conn.createStatement(); 19 ResultSet rs = stmt.executeQuery("select * from t_users"); 20 %> 21 <table bgcolor="#9999dd" border="1" width="300px"> 22 <% 23 while(rs.next()) 24 { 25 %> 26 <tr> 27 <td>循环值:</td> 28 <td><%=rs.getString(1)%></td> 29 <td><%=rs.getString(2)%></td> 30 </tr> 31 <% 32 } 33 %> 34 </table> 35 </body> 36 </html>

要先下载jdbc驱动放到tomcat的lib或项目的lib下;

JSP直接连接sql2008数据库并显示

标签:

人气教程排行