有与数据库相连的问题一直无法得到解决,非常的郁闷!
在Tomcat Project 项目下建一个index.html和show.jsp,功能是点击index.html中的超链接,连到show.jsp,即显示SQL SERVER中users表中数据
index.html 代码是:
---------------------------
<body>
显示users表中信息
<center>
<a href=show.jsp>显示users表中信息</a></center></body>
---------------------------
show.jsp代码是:
<body>
<%String username =null;
String password = null;
try{
/*
* 连接数据库
*/
out.println("<br>begin:<br>");
System.out.println("<br>begin:<br>");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:sqlserver","sa","sa");
Statement stmt=conn.createStatement();
String sql = "select * from users";
ResultSet rs=stmt.executeQuery( sql ); out.println("<br>end:<br>"); while(rs.next()){ out.println("the username is:"+rs.getString("username"));
}
}catch(Exception ee){
ee.printStackTrace();
}
%>
<br><%=username %>
<br>
<%=password %></body>
-------------------------
结果显示:
begin:null
nullusers表格中的数据显示不出来,end也没读出来
请大家帮我看看是那边出现了问题
在此表示感谢大家的帮助………………
在Tomcat Project 项目下建一个index.html和show.jsp,功能是点击index.html中的超链接,连到show.jsp,即显示SQL SERVER中users表中数据
index.html 代码是:
---------------------------
<body>
显示users表中信息
<center>
<a href=show.jsp>显示users表中信息</a></center></body>
---------------------------
show.jsp代码是:
<body>
<%String username =null;
String password = null;
try{
/*
* 连接数据库
*/
out.println("<br>begin:<br>");
System.out.println("<br>begin:<br>");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:sqlserver","sa","sa");
Statement stmt=conn.createStatement();
String sql = "select * from users";
ResultSet rs=stmt.executeQuery( sql ); out.println("<br>end:<br>"); while(rs.next()){ out.println("the username is:"+rs.getString("username"));
}
}catch(Exception ee){
ee.printStackTrace();
}
%>
<br><%=username %>
<br>
<%=password %></body>
-------------------------
结果显示:
begin:null
nullusers表格中的数据显示不出来,end也没读出来
请大家帮我看看是那边出现了问题
在此表示感谢大家的帮助………………
你配置了ODBC数据源了吗??