packagedbBean;importjava.sql.*;publicclassDBBean{privateStringdriverStr="sun.jdbc.odbc.JdbcOdbcDriver";privateStringconnStr="jdbc:odbc:mydbsource";privateConnectionconn=n...
package dbBean;
import java.sql.*;
public class DBBean
{
private String driverStr = "sun.jdbc.odbc.JdbcOdbcDriver";
private String connStr = "jdbc:odbc:mydbsource";
private Connection conn = null;
private Statement stmt = null;
public DBBean()
{
try {
Class.forName(driverStr);
}
catch(ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
public void setDriverStr(String dstr)
{
driverStr=dstr;
}
public void setConnStr(String cstr)
{
connStr=cstr;
}
public ResultSet executeQuery(String sql)
{
ResultSet rs = null;
try {
conn = DriverManager.getConnection(connStr);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.out.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql)
{
int result=0;
try{
conn = DriverManager.getConnection(connStr);
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
return result;
}
public void close()
{
try{
stmt.close();
conn.close();
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
}
}
注:上面代码实现的是使用Access数据源与Jsp相连。
另:这次使用的不是数据源,而是MySql下的数据库Book。
最好能说明一下怎么在JSP中引入和调用,先谢谢了,如果问题解决会有更高的加分。
request.setCharacterEncoding("gb2312");
session.setMaxInactiveInterval(-1);
String user=request.getParameter("user");
String pw=request.getParameter("pw");
String sql="select * from user where name='"+user+"' and password='"+pw+"'";
ResultSet rs=conn.executeQuery(sql);
if(rs.next())
{
session.setAttribute("rank",rs.getString("rank"));
session.setAttribute("id",rs.getString("userid"));
session.setAttribute("name",rs.getString("name"));
rs.close();
conn.close();
%>
}
else
{
rs.close();
conn.close();
%>
}
%>
请问:如果上面的页面要调用bean,前面应该怎么写?
展开