javabean连接数据库问题
<%@pagecontentType="text/html;charset=gb2312"import="java.sql.*"%><jsp:useBeanid="dbc...
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*" %>
<jsp:useBean id="dbconn" scope="page" class="myapp.DBConnection"/>
<html>
<head>
<title>数据查询</title>
</head>
<body bgcolor="#FFFF00">
<p align="center">用户信息表</p>
<hr>
<table border="0" align="center">
<tr bgcolor="#ccff22" align="center">
<td>用户名</td>
<td>密码</td>
<td>性别</td>
<td>年级</td>
<td>院系</td>
<td>专业</td>
<td>住址</td>
<td>邮箱</td>
</tr>
<%
dbconn.getConnection();
String sql="select * from userInfo";
ResultSet rs=dbconn.executeQuery(sql);
while(rs.next()){
%>
<tr align="left">
<td><%=rs.getString("username")%></td>
<td><%=rs.getString("password")%></td>
<td><%=rs.getString("sex")%></td>
<td><%=rs.getString("grade")%></td>
<td><%=rs.getString("department")%></td>
<td><%=rs.getString("speciality")%></td>
<td><%=rs.getString("address")%></td>
<td><%=rs.getString("email")%></td>
</tr>
<%}
dbconn.closeConn();
%>
</table>
</body>
</html>
总是出错啊。。
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /user_select.jsp at line 25
22: dbconn.getConnection();
23: String sql="select * from userInfo";
24: ResultSet rs=dbconn.executeQuery(sql);
25: while(rs.next()){
26: %>
27: <tr align="left">
28: <td><%=rs.getString("username")%></td>
是不是javabean写错了啊,但是查了很多遍也没错呀。。
1楼同志说的方法行不通啊。。 展开
<jsp:useBean id="dbconn" scope="page" class="myapp.DBConnection"/>
<html>
<head>
<title>数据查询</title>
</head>
<body bgcolor="#FFFF00">
<p align="center">用户信息表</p>
<hr>
<table border="0" align="center">
<tr bgcolor="#ccff22" align="center">
<td>用户名</td>
<td>密码</td>
<td>性别</td>
<td>年级</td>
<td>院系</td>
<td>专业</td>
<td>住址</td>
<td>邮箱</td>
</tr>
<%
dbconn.getConnection();
String sql="select * from userInfo";
ResultSet rs=dbconn.executeQuery(sql);
while(rs.next()){
%>
<tr align="left">
<td><%=rs.getString("username")%></td>
<td><%=rs.getString("password")%></td>
<td><%=rs.getString("sex")%></td>
<td><%=rs.getString("grade")%></td>
<td><%=rs.getString("department")%></td>
<td><%=rs.getString("speciality")%></td>
<td><%=rs.getString("address")%></td>
<td><%=rs.getString("email")%></td>
</tr>
<%}
dbconn.closeConn();
%>
</table>
</body>
</html>
总是出错啊。。
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /user_select.jsp at line 25
22: dbconn.getConnection();
23: String sql="select * from userInfo";
24: ResultSet rs=dbconn.executeQuery(sql);
25: while(rs.next()){
26: %>
27: <tr align="left">
28: <td><%=rs.getString("username")%></td>
是不是javabean写错了啊,但是查了很多遍也没错呀。。
1楼同志说的方法行不通啊。。 展开
2个回答
展开全部
dbconn.getConnection();
String sql="select * from userInfo";
ResultSet rs=dbconn.executeQuery(sql);
先得创建一个Statement对像再执行 sql语句。
ResultSet rs=dbconn.createStatement().executeQuerty(sql);
String sql="select * from userInfo";
ResultSet rs=dbconn.executeQuery(sql);
先得创建一个Statement对像再执行 sql语句。
ResultSet rs=dbconn.createStatement().executeQuerty(sql);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我写个给你
你在外面调用我的,
package common;
import java.sql.*;
public class DBManager {
private String myDriver=null;
private String myConnStr=null;
private Connection myConn=null;
private Statement myStmt=null;
private ResultSet myResults=null;
public DBManager(){ //加载sql驱动
myDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
myConnStr="jdbc:microsoft:sqlserver://localhost:1433;"+"DatabaseName=sport_ntu;User=sa;PAssword=123456";
//myDriver="sun.jdbc.odbc.JdbcOdbcDriver";
//myConnStr="jdbc:odbc:sport_ntu"+""+"";
try{
Class.forName(myDriver);
}catch(ClassNotFoundException e){
System.out.println("加载sql驱动"+e.getMessage());
}
}
public boolean execInsert(String insertString){ //添加数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myStmt.executeUpdate(insertString);
} catch(SQLException e){
System.out.println("添加数据"+e.getMessage());
return false;
}
return true;
}
public ResultSet execQuery(String queryString){ //查询数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myResults=myStmt.executeQuery(queryString);
} catch(SQLException ex){
System.out.println("查询数据"+ex.getMessage());
}
return myResults;
}
public boolean execUpdata(String updataString){ //更新数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myStmt.executeUpdate(updataString);
myStmt.close();
myConn.close();
} catch(SQLException ex){
System.out.println("更新数据"+ex.getMessage());
return false;
}
return false;
}
public void execDelect(String deleteString){ //删除数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myStmt.executeUpdate(deleteString); //executeDelect
myStmt.close();
myConn.close();
} catch(SQLException ex){
System.out.println("删除数据"+ex.getMessage());
}
}
public void closeManager(){ //关闭创建对象
try{
myStmt.close();
myConn.close();
} catch(SQLException ex){
ex.printStackTrace();
}
}
public void closeStatement(){
try{
myStmt.close();
} catch(SQLException ex){
ex.printStackTrace();
}
}
public void closeConnection(){
try{
myConn.close();
} catch(SQLException ex){
ex.printStackTrace();
}
}
}
你在外面调用我的,
package common;
import java.sql.*;
public class DBManager {
private String myDriver=null;
private String myConnStr=null;
private Connection myConn=null;
private Statement myStmt=null;
private ResultSet myResults=null;
public DBManager(){ //加载sql驱动
myDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
myConnStr="jdbc:microsoft:sqlserver://localhost:1433;"+"DatabaseName=sport_ntu;User=sa;PAssword=123456";
//myDriver="sun.jdbc.odbc.JdbcOdbcDriver";
//myConnStr="jdbc:odbc:sport_ntu"+""+"";
try{
Class.forName(myDriver);
}catch(ClassNotFoundException e){
System.out.println("加载sql驱动"+e.getMessage());
}
}
public boolean execInsert(String insertString){ //添加数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myStmt.executeUpdate(insertString);
} catch(SQLException e){
System.out.println("添加数据"+e.getMessage());
return false;
}
return true;
}
public ResultSet execQuery(String queryString){ //查询数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myResults=myStmt.executeQuery(queryString);
} catch(SQLException ex){
System.out.println("查询数据"+ex.getMessage());
}
return myResults;
}
public boolean execUpdata(String updataString){ //更新数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myStmt.executeUpdate(updataString);
myStmt.close();
myConn.close();
} catch(SQLException ex){
System.out.println("更新数据"+ex.getMessage());
return false;
}
return false;
}
public void execDelect(String deleteString){ //删除数据
try{
myConn=DriverManager.getConnection(myConnStr);
myStmt=myConn.createStatement();
myStmt.executeUpdate(deleteString); //executeDelect
myStmt.close();
myConn.close();
} catch(SQLException ex){
System.out.println("删除数据"+ex.getMessage());
}
}
public void closeManager(){ //关闭创建对象
try{
myStmt.close();
myConn.close();
} catch(SQLException ex){
ex.printStackTrace();
}
}
public void closeStatement(){
try{
myStmt.close();
} catch(SQLException ex){
ex.printStackTrace();
}
}
public void closeConnection(){
try{
myConn.close();
} catch(SQLException ex){
ex.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询