日期:2014-05-18  浏览次数:20735 次

急求一个jsp+javabean连接sqlserver的实例源码?????
问题同上,要对的呀,小弟不胜感激

------解决方案--------------------
package firm;
import java.sql.*;
public class firm{
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String sConnStr = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=firm ";
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
private String user= "sa ";
private String pass= "qwe ";
public firm() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn=DriverManager.getConnection(sConnStr,user,pass);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println( "aq.executeQuery: " + ex.getMessage());

}
return rs;
}
public void executeUpdate(String sql) {


try
{
conn= DriverManager.getConnection(sConnStr,user,pass);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);

}
catch (SQLException ex) {
System.err.println( "aq.executeUpdate: " + ex.getMessage());

}
}
public void closeStmt(){
try
{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try
{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
------解决方案--------------------
javabean方式:
<%@ page contentType= "text/html;charset=gb2312 " %>
<%@ page language= "java " import= "java.sql.* " %>
<jsp:useBean id= "mhBean " scope= "page " class= "firm.firm "/>
<%
String managername= new String(request.getParameter( "managername ").getBytes( "ISO8859_1 "));
String managerpass= new String(request.getParameter( "managerpass ").getBytes( "ISO8859_1 "));
String sql= "select * from admin where adminname= ' "+managername+ " 'and adminpwd= ' "+managerpass+ " ' ";
ResultSet rs=mhBean.executeQuery(sql);
if(rs.next()) {
rs.close();
mhBean.closeConn();
session.setAttribute( "managername ",managername);
response.sendRedirect( "manage.jsp ");
%>
<% }
else {
rs.close();
mhBean.closeConn();

out.print( "用户名或密码错误!请重新输入! ");
response.setHeader( "Refresh ", "3;url=http://localhost:8080/aa/manager_login.jsp ");
}
%>
------解决方案--------------------
firm 包没有导入!! 老兄,类名要大写!!

------解决方案--------------------
firm f = new firm();

这个firm 类应该是找不到!!导入包!!