通过JSP连接数据库的JDBC
<%@ page contentType="text/xml; charset=gb2312" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<Result>
<%
/*
* @author Evangelion
* @date
* @version V 1.0
*/
Connection con=null;//连接变量
Statement stmt=null;//状态变量
ResultSet rs=null;//结果变量
String url;//连接所需要的地址
try{
Class.forName("com.mysql.jdbc.Driver").newInstance(); //所需要的jdbc驱动
url ="jdbc:mysql://localhost:3306/flex"; // 请自动换做你jsp的地址,因为我xml中在文件列表默认读取index.jsp所以就没写
con= DriverManager.getConnection(url,"root","root"); //进行连接
//这句非常重要,表明stmt生成的ResultSet的指针可以来回滚动。
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from linechartCall");//括号内请写上和你数据库操作查询的语句
//以下代码用来将结果以xml的格式来展现。读取节点数据
while (rs.next())
{
%>
<callInc>
<hours><%=rs.getString(1)%></hours>
<lastweekValue><%=rs.getString(2)%></lastweekValue>
<yesterdayValue><%=rs.getString(3)%></yesterdayValue>
<value><%=rs.getString(4)%></value>
</callInc>
<%
}
rs.close();
stmt.close();
con.close();//一定要记得关闭数据库不然很容易使数据库崩溃
}
catch(Exception ex)
{
out.println(ex.toString());
rs.close();
stmt.close();
con.close();
}
%>
</Result>