当前位置: 技术问答>java相关
请大虾给几个在JavaBean中访问数据库的列子。。。
来源: 互联网 发布时间:2015-04-14
本文导语: JSP初学者,不想把数据库操作写在JSP页面里,想用JavaBean把数据库操作封装了。 下载的资料将得很含糊,没看明白,试写了几个也都报错而告终。 请大虾给在JavaBean中访问数据库(直接在JavaBean中查询数据库)的列子...
JSP初学者,不想把数据库操作写在JSP页面里,想用JavaBean把数据库操作封装了。
下载的资料将得很含糊,没看明白,试写了几个也都报错而告终。
请大虾给在JavaBean中访问数据库(直接在JavaBean中查询数据库)的列子。
先谢了。
下载的资料将得很含糊,没看明白,试写了几个也都报错而告终。
请大虾给在JavaBean中访问数据库(直接在JavaBean中查询数据库)的列子。
先谢了。
|
转载:
改动一下就可以实现需要的数据库操作功能:
import java.sql.*;
/**
* Title: 数据库封装
* Description:
* Copyright: Copyright (c) 2001
* Company: MICHEAL
* @author luojia
* @version 1.0
*/
public class Mssql {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private String dbdriver = "org.gjt.mm.mysql.Driver";
private String dburl = "jdbc:mysql://127.0.0.1/stock";
/**
* 构造数据库的连接和访问类
*/
public Mssql() throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
stmt = conn.createStatement();
}
public Mssql(String sql) throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
this.prepareStatement(sql);
}
public Mssql(int resultSetType, int resultSetConcurrency) throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
/**
* 设置对应值
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException {
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException {
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException {
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException {
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
prepstmt.setFloat(index,value);
}
public void clearParameters()
throws SQLException
{
prepstmt.clearParameters();
}
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 关闭连接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
改动一下就可以实现需要的数据库操作功能:
import java.sql.*;
/**
* Title: 数据库封装
* Description:
* Copyright: Copyright (c) 2001
* Company: MICHEAL
* @author luojia
* @version 1.0
*/
public class Mssql {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private String dbdriver = "org.gjt.mm.mysql.Driver";
private String dburl = "jdbc:mysql://127.0.0.1/stock";
/**
* 构造数据库的连接和访问类
*/
public Mssql() throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
stmt = conn.createStatement();
}
public Mssql(String sql) throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
this.prepareStatement(sql);
}
public Mssql(int resultSetType, int resultSetConcurrency) throws Exception {
Class.forName(dbdriver);
conn = DriverManager.getConnection(dburl,"myuser","mypd");
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
/**
* 设置对应值
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException {
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException {
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException {
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException {
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
prepstmt.setFloat(index,value);
}
public void clearParameters()
throws SQLException
{
prepstmt.clearParameters();
}
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 关闭连接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
|
以下不知道何时抄下来的,来自于网上的资料,很不错,试试看吧
原文:
数据库连接方法:(其中汉字部分根据自己的设置修改)
建立连接前请确认已在CLASSPATH加载数据库连接驱动程序,数据库监听服务已启动.
1.Oracle
public void ConnectToDatabase() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@数据库服务器IP地址:1521:数据库SID名";
con=DriverManager.getConnection(url,"数据库使用者","使用者密码");
}
catch (SQLException e) {}
catch (Exception e) {}
}
2.SysBase
public void ConnectToDatabase() {
try {
String driverName = "com.sybase.jdbc.SybDriver";
Driver driver = (Driver)Class.forName(driverName).newInstance();
Properties p = new Properties();
p.put("user","数据库使用者");
p.put("password","使用者密码");
String dbURL = "jdbc:sybase:Tds:数据库服务器IP地址:4096/数据库SID名";
connection = driver.connect(dbURL,p);
}
catch (SQLException e) {}
catch (Exception e) {}
}
3.MySql
public void ConnectToDatabase() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url=="jdbc:mysql://数据库服务器IP地址/authority?user=数据库使用者;password=数据库使用者;"
con=DriverManager.getConnection(URL);
}
catch (SQLException e) {}
catch (Exception e) {}
}
4.Access (WIN2000/NT/98)
public void ConnectToDatabase() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:数据源名";
con=DriverManager.getConnection(url,"使用者","使用者密码");
}
catch (SQLException e) {}
catch (Exception e) {}
}
添加数据源的方法:
控制面板-->管理工具-->数据源(ODBC)-->系统DNS-->添加
高级选项里可设置登录名和密码.如无则建立连接的语法为con=DriverManager.getConnection(url);
5.SqlServer
public void ConnectToDatabase() {
try {
Class.forName("com.merant.datadirect.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:sqlserver://数据库服务器IP地址:1433;databasename=数据库名";
con=DriverManager.getConnection(url,"数据库用户名","数据库用户密码");
}
catch (SQLException e) {
}
catch (Exception e) {
}
}
原文:
数据库连接方法:(其中汉字部分根据自己的设置修改)
建立连接前请确认已在CLASSPATH加载数据库连接驱动程序,数据库监听服务已启动.
1.Oracle
public void ConnectToDatabase() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@数据库服务器IP地址:1521:数据库SID名";
con=DriverManager.getConnection(url,"数据库使用者","使用者密码");
}
catch (SQLException e) {}
catch (Exception e) {}
}
2.SysBase
public void ConnectToDatabase() {
try {
String driverName = "com.sybase.jdbc.SybDriver";
Driver driver = (Driver)Class.forName(driverName).newInstance();
Properties p = new Properties();
p.put("user","数据库使用者");
p.put("password","使用者密码");
String dbURL = "jdbc:sybase:Tds:数据库服务器IP地址:4096/数据库SID名";
connection = driver.connect(dbURL,p);
}
catch (SQLException e) {}
catch (Exception e) {}
}
3.MySql
public void ConnectToDatabase() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url=="jdbc:mysql://数据库服务器IP地址/authority?user=数据库使用者;password=数据库使用者;"
con=DriverManager.getConnection(URL);
}
catch (SQLException e) {}
catch (Exception e) {}
}
4.Access (WIN2000/NT/98)
public void ConnectToDatabase() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:数据源名";
con=DriverManager.getConnection(url,"使用者","使用者密码");
}
catch (SQLException e) {}
catch (Exception e) {}
}
添加数据源的方法:
控制面板-->管理工具-->数据源(ODBC)-->系统DNS-->添加
高级选项里可设置登录名和密码.如无则建立连接的语法为con=DriverManager.getConnection(url);
5.SqlServer
public void ConnectToDatabase() {
try {
Class.forName("com.merant.datadirect.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:sqlserver://数据库服务器IP地址:1433;databasename=数据库名";
con=DriverManager.getConnection(url,"数据库用户名","数据库用户密码");
}
catch (SQLException e) {
}
catch (Exception e) {
}
}