当前位置: 技术问答>java相关
谁给我个调用数据库的Bean?
来源: 互联网 发布时间:2015-05-26
本文导语: 谁给我个调用数据库的Bean? | import java.util.*; import java.sql.*; import java.io.*; public class DbConnection { Connection con=null; Statement smt=null; ResultSet rt=null; public DbConnection() { } public boolean openConnec...
谁给我个调用数据库的Bean?
|
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbConnection
{
Connection con=null;
Statement smt=null;
ResultSet rt=null;
public DbConnection()
{
}
public boolean openConnection()
{
Properties prop=new Properties();
try
{
InputStream in=getClass().getResourceAsStream("db.properties");
prop.load(in);
if(in!=null)
{
in.close();
}
}
catch(IOException ioe)
{
System.out.println("Con not open DataBase!");
}
String dr=prop.getProperty("dr");
String ul=prop.getProperty("ul");
String usr=prop.getProperty("usr");
String pwd=prop.getProperty("pwd");
System.out.println("dr=["+dr+"]");
System.out.println("ul=["+ul+"]");
System.out.println("usr=["+usr+"]");
System.out.println("pwd=["+pwd+"]");
try
{
Class.forName(dr);
}
catch(ClassNotFoundException cnfe)
{
System.out.println("driver "+dr+"load failed!");
return false;
}
try
{
this.con=DriverManager.getConnection(ul,usr,pwd);
}
catch(SQLException sqle2)
{
System.out.println("Connection failed!");
}
return true;
}
public ResultSet executeQuery(String query) throws SQLException
{
this.smt=con.createStatement();
this.rt=smt.executeQuery(query);
return rt;
}
public void executeUpdate(String query) throws SQLException
{
this.smt=con.createStatement();
smt.executeUpdate(query);
if(smt!=null)
{
smt.close();
}
}
public void close() throws SQLException
{
if(con!=null) con.close();
if(rt!=null) rt.close();
if(smt!=null) smt.close();
}
protected void finalize() throws Throwable
{
this.close();
}
}
/*............*/
import java.sql.*;
public class ViewQueryBean
{
DbConnection dc=null;
ResultSet rt=null;
public ViewQueryBean()
{
dc=new DbConnection();
}
public boolean openConnection()
{
return dc.openConnection();
}
public void executeQuery(String sqlsmt) throws SQLException
{
this.rt=dc.executeQuery(sqlsmt);
}
public void executeUpdate(String sqlsmt) throws SQLException
{
dc.executeUpdate(sqlsmt);
}
public int getColumnCount() throws SQLException
{
ResultSetMetaData rtmd=rt.getMetaData();
return rtmd.getColumnCount();
}
public String getColumnName(int index) throws SQLException
{
ResultSetMetaData rtmd=rt.getMetaData();
return rtmd.getColumnName(index);
}
public String getData(int index) throws SQLException
{
return rt.getString(index).trim();
}
public String getData(String columnName) throws SQLException
{
return rt.getString(columnName).trim();
}
public void close() throws SQLException
{
if(rt!=null) rt.close();
if(dc!=null) dc.close();
}
protected void finalize() throws Throwable
{
close();
}
}
import java.sql.*;
import java.io.*;
public class DbConnection
{
Connection con=null;
Statement smt=null;
ResultSet rt=null;
public DbConnection()
{
}
public boolean openConnection()
{
Properties prop=new Properties();
try
{
InputStream in=getClass().getResourceAsStream("db.properties");
prop.load(in);
if(in!=null)
{
in.close();
}
}
catch(IOException ioe)
{
System.out.println("Con not open DataBase!");
}
String dr=prop.getProperty("dr");
String ul=prop.getProperty("ul");
String usr=prop.getProperty("usr");
String pwd=prop.getProperty("pwd");
System.out.println("dr=["+dr+"]");
System.out.println("ul=["+ul+"]");
System.out.println("usr=["+usr+"]");
System.out.println("pwd=["+pwd+"]");
try
{
Class.forName(dr);
}
catch(ClassNotFoundException cnfe)
{
System.out.println("driver "+dr+"load failed!");
return false;
}
try
{
this.con=DriverManager.getConnection(ul,usr,pwd);
}
catch(SQLException sqle2)
{
System.out.println("Connection failed!");
}
return true;
}
public ResultSet executeQuery(String query) throws SQLException
{
this.smt=con.createStatement();
this.rt=smt.executeQuery(query);
return rt;
}
public void executeUpdate(String query) throws SQLException
{
this.smt=con.createStatement();
smt.executeUpdate(query);
if(smt!=null)
{
smt.close();
}
}
public void close() throws SQLException
{
if(con!=null) con.close();
if(rt!=null) rt.close();
if(smt!=null) smt.close();
}
protected void finalize() throws Throwable
{
this.close();
}
}
/*............*/
import java.sql.*;
public class ViewQueryBean
{
DbConnection dc=null;
ResultSet rt=null;
public ViewQueryBean()
{
dc=new DbConnection();
}
public boolean openConnection()
{
return dc.openConnection();
}
public void executeQuery(String sqlsmt) throws SQLException
{
this.rt=dc.executeQuery(sqlsmt);
}
public void executeUpdate(String sqlsmt) throws SQLException
{
dc.executeUpdate(sqlsmt);
}
public int getColumnCount() throws SQLException
{
ResultSetMetaData rtmd=rt.getMetaData();
return rtmd.getColumnCount();
}
public String getColumnName(int index) throws SQLException
{
ResultSetMetaData rtmd=rt.getMetaData();
return rtmd.getColumnName(index);
}
public String getData(int index) throws SQLException
{
return rt.getString(index).trim();
}
public String getData(String columnName) throws SQLException
{
return rt.getString(columnName).trim();
}
public void close() throws SQLException
{
if(rt!=null) rt.close();
if(dc!=null) dc.close();
}
protected void finalize() throws Throwable
{
close();
}
}
|
package com.davidflanagan.examples.sql;
import java.sql.*;
import java.util.Properties;
/**
* This class uses the DatabaseMetaData class to obtain information about
* the database, the JDBC driver, and the tables in the database, or about
* the columns of a named table.
**/
public class GetDBInfo {
public static void main(String[] args) {
Connection c = null; // The JDBC connection to the database server
try {
// Look for the properties file DB.props in the same directory as
// this program. It will contain default values for the various
// parameters needed to connect to a database
Properties p = new Properties();
try { p.load(GetDBInfo.class.getResourceAsStream("DB.props")); }
catch (Exception e) {}
// Get default values from the properties file
String driver = p.getProperty("driver"); // Driver class name
String server = p.getProperty("server", ""); // JDBC URL for server
String user = p.getProperty("user", ""); // db user name
String password = p.getProperty("password", ""); // db password
// These variables don't have defaults
String database = null; // The db name (appended to server URL)
String table = null; // The optional name of a table in the db
// Parse the command-line args to override the default values above
for(int i = 0; i
import java.sql.*;
import java.util.Properties;
/**
* This class uses the DatabaseMetaData class to obtain information about
* the database, the JDBC driver, and the tables in the database, or about
* the columns of a named table.
**/
public class GetDBInfo {
public static void main(String[] args) {
Connection c = null; // The JDBC connection to the database server
try {
// Look for the properties file DB.props in the same directory as
// this program. It will contain default values for the various
// parameters needed to connect to a database
Properties p = new Properties();
try { p.load(GetDBInfo.class.getResourceAsStream("DB.props")); }
catch (Exception e) {}
// Get default values from the properties file
String driver = p.getProperty("driver"); // Driver class name
String server = p.getProperty("server", ""); // JDBC URL for server
String user = p.getProperty("user", ""); // db user name
String password = p.getProperty("password", ""); // db password
// These variables don't have defaults
String database = null; // The db name (appended to server URL)
String table = null; // The optional name of a table in the db
// Parse the command-line args to override the default values above
for(int i = 0; i