一个简单数据库连接的javabean
时间:2006-12-22 来源:kvsg_yang
DBConnection.java
package ConnectionDB;
import java.sql.*;
public class DBConnection
{
private String driver;
private StringBuffer accessURL;
private String accessFileName;
private String accessFilePath;
private Connection conn;
public DBConnection()
{
driver="sun.jdbc.odbc.JdbcOdbcDriver";
accessURL=new StringBuffer("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=");
accessFilePath="";
accessFileName="";
conn=null;
}
public Connection getConnectionToAccess(String s)throws SQLException,ClassNotFoundException{
accessURL.append(s);
Class.forName(driver);
conn=DriverManager.getConnection(accessURL.toString());
return conn;
}
public void close()
{
try{
conn.close();
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
}
}
} test.jsp <%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<jsp:useBean id="DBConnection" scope="page" class="ConnectionDB.DBConnection"/>
<%
Connection conn=DBConnection.getConnectionToAccess(application.getRealPath("data/guestbook.mdb"));
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
%> <%
String sql="select * from Guestbook order by id DESC";
ResultSet Rs=stmt.executeQuery(sql);
while(Rs.next()) {%>
您的第一个字段内容为:<%=Rs.getString(1)%>
您的第二个字段内容为:<%=Rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%Rs.close();
stmt.close();
conn.close();
%>
public class DBConnection
{
private String driver;
private StringBuffer accessURL;
private String accessFileName;
private String accessFilePath;
private Connection conn;
public DBConnection()
{
driver="sun.jdbc.odbc.JdbcOdbcDriver";
accessURL=new StringBuffer("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=");
accessFilePath="";
accessFileName="";
conn=null;
}
public Connection getConnectionToAccess(String s)throws SQLException,ClassNotFoundException{
accessURL.append(s);
Class.forName(driver);
conn=DriverManager.getConnection(accessURL.toString());
return conn;
}
public void close()
{
try{
conn.close();
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
}
}
} test.jsp <%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<jsp:useBean id="DBConnection" scope="page" class="ConnectionDB.DBConnection"/>
<%
Connection conn=DBConnection.getConnectionToAccess(application.getRealPath("data/guestbook.mdb"));
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
%> <%
String sql="select * from Guestbook order by id DESC";
ResultSet Rs=stmt.executeQuery(sql);
while(Rs.next()) {%>
您的第一个字段内容为:<%=Rs.getString(1)%>
您的第二个字段内容为:<%=Rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%Rs.close();
stmt.close();
conn.close();
%>
相关阅读 更多 +