当前位置: 技术问答>java相关
请帮我看一个jdbc bean的代码
来源: 互联网 发布时间:2015-09-01
本文导语: import java.sql.*; public class jdbcBean{ private String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; private String connStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JSPDB"; private Connection conn=null; private Statement stmt=nu...
import java.sql.*;
public class jdbcBean{
private String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String connStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JSPDB";
private Connection conn=null;
private Statement stmt=null;
private String username=null;
private String password=null;
private ResultSet rs=null;
private void init() throws SQLException{
try{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(connStr,username,password);
stmt=conn.createStatement();
}
catch(ClassNotFoundException e){
System.err.println("There is not JDBC driver!");
}
}
public void connect(String username,String password) throws SQLException{
this.username=username;
this.password=password;
init();
}
public ResultSet executeQuery(String sqlStatement){
rs=null;
try{
rs=stmt.executeQuery(sqlStatement);
}
catch(SQLException ex){
System.err.println("SQLRun:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sqlStatement){
try{
stmt.executeUpdate(sqlStatement);
}
catch(SQLException ex){
System.err.println("SQLRun:"+ex.getMessage());
}
}
public void close(){
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException ex){
}
}
}
我的jsp页面里直接用jdbc连接没有问题,也就是driver没有问题。但是我在jsp里调用这个Bean就是不行,请帮忙看看上面的代码。
我的调用jsp:
public class jdbcBean{
private String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String connStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JSPDB";
private Connection conn=null;
private Statement stmt=null;
private String username=null;
private String password=null;
private ResultSet rs=null;
private void init() throws SQLException{
try{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(connStr,username,password);
stmt=conn.createStatement();
}
catch(ClassNotFoundException e){
System.err.println("There is not JDBC driver!");
}
}
public void connect(String username,String password) throws SQLException{
this.username=username;
this.password=password;
init();
}
public ResultSet executeQuery(String sqlStatement){
rs=null;
try{
rs=stmt.executeQuery(sqlStatement);
}
catch(SQLException ex){
System.err.println("SQLRun:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sqlStatement){
try{
stmt.executeUpdate(sqlStatement);
}
catch(SQLException ex){
System.err.println("SQLRun:"+ex.getMessage());
}
}
public void close(){
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException ex){
}
}
}
我的jsp页面里直接用jdbc连接没有问题,也就是driver没有问题。但是我在jsp里调用这个Bean就是不行,请帮忙看看上面的代码。
我的调用jsp:
|
我也是一个新手,不能帮你解决上面到底出了什么问题,我这里有一个,跟你用的驱动一样的,我写成bean是好用的,你参考参考了。
另外我觉得连接的信息写在jsp文件里面不是很好吧?觉得最好是封装在bean里面
package xmlBean;
import java.sql.*;
public class mssqlDB{
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr = "jdbc:microsoft:sqlserver://Q2A8U3:1433";
String sUsernameStr="pjresearch";
String sPassStr="1234";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public mssqlDB(){
try{
Class.forName(sDBDriver); //返回一个给出了完整名字的class对象
}
catch(java.lang.ClassNotFoundException e){
System.err.println("mssqlDB(): " + e.getMessage());
}
}
public void executeInsert(String sql){ //插入数据的方法
try{
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.err.println("mssqlDB.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("mssqlDB.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
try {
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void executeDelete(String sql){
try{
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.err.println("mssqlDB.executeDelete:"+ex.getMessage());
}
}
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
另外我觉得连接的信息写在jsp文件里面不是很好吧?觉得最好是封装在bean里面
package xmlBean;
import java.sql.*;
public class mssqlDB{
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr = "jdbc:microsoft:sqlserver://Q2A8U3:1433";
String sUsernameStr="pjresearch";
String sPassStr="1234";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public mssqlDB(){
try{
Class.forName(sDBDriver); //返回一个给出了完整名字的class对象
}
catch(java.lang.ClassNotFoundException e){
System.err.println("mssqlDB(): " + e.getMessage());
}
}
public void executeInsert(String sql){ //插入数据的方法
try{
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.err.println("mssqlDB.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("mssqlDB.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
try {
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void executeDelete(String sql){
try{
conn = DriverManager.getConnection(sConnStr,sUsernameStr,sPassStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex){
System.err.println("mssqlDB.executeDelete:"+ex.getMessage());
}
}
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
|
Add a line in the bean:
package jdbctest; // add here
import java.sql.*;
public class jdbcBean{
private String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
.......
JSP page:
And you also should know where to put the bean file.
package jdbctest; // add here
import java.sql.*;
public class jdbcBean{
private String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
.......
JSP page:
And you also should know where to put the bean file.
|
你应该确定你的bean的位置方的没有问题。
对了bean还的编译
web-infclassesjdbcBean.class
对了bean还的编译
web-infclassesjdbcBean.class
|
同意楼上的,把bean放在包里,在bean里写package jdbctest;
然后在classes目录里新建一个jdbctest目录,把编译后的bean放在里面。
jsp页面引用的时候写
然后在classes目录里新建一个jdbctest目录,把编译后的bean放在里面。
jsp页面引用的时候写