JAVA问题s,读可dbtools以后,几年前的问题都拿出来问了(100分)

C

CJ

Unregistered / Unconfirmed
GUEST, unregistred user!
/*******************************************************
After read the dbtool by Dr. yysun, I commentED this program
and have a little foolish questionS should ask, thanx a lot.
Program, comment and questions are all in below::
*******************************************************/
// (C) 2000.3.8 by Yiyi Sun
// Sorry
// Poorly commented by CJ 2000.3.12
package delphibbs;
import java.text.*;
import java.util.*;
import java.sql.*;
public class dbtools
{
//Set both DBDriver name and Connection String
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:eek:dbc:delphibbs";
//JDBC Connection
Connection conn = null;
//SQL Statement Object
Statement stmt = null;
//ResultSet returned
ResultSet rs = null;
/*********************************
* java.lang.Object
* |
* +--java.util.Dictionary
* |
* +--java.util.Hashtable
* <b>
* Hashtable is so unearthliness
* forgive me use word like this
* I know it can store some object.
* Like TList?
* </b>
***********************************/
Hashtable room_list = new Hashtable();
Hashtable col_data = new Hashtable();
/********************************************************
* This mathod open the connect to Database with specified
* DB Driver
* It's also the construction mathod for the class dbtools
* forName(String?className)
* This mathod returns the Class object associated with
* the class or interface with the given string name.
* <b>
* Q:Is here test the DB Driver? if so, perhaps it's not
* nessary, or it's really nessary? can I skip this step?
* if cannot, what these excatlydo
?
* I know HOW but I should know WHY
* </b>
********************************************************/
public dbtools()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("dbtools.openConn: " + e.getMessage());
}
}
/***********************************************
* This mathod search a string for '/' and replace
* it into '//' because '/' is a special character
* in both C/C++ and Java
***********************************************/
public String chkStr(String inStr)
{
StringBuffer result = new StringBuffer("");
char cc;
if (inStr != null)
{
for (int i = 0;
i < inStr.length();
i++)
{
cc = inStr.charAt(i);
if (cc == '/'')
{
result.append('/'');
}
result.append(cc);
}
}
return result.toString();
}
/************************************************************
* This mathod execute a SQL Query with specified SQL statement
* It returns a RecordSet object
************************************************************/
public ResultSet executeQuery(String sql)
{
// Set Result Set to null
rs = null;
try
{
//Get the connection with the connection string
//declared globly above
//<b>Q:Is the connection is already opened in the
// constructor</b>
conn = DriverManager.getConnection(sConnStr);
//Create the statement object
stmt = conn.createStatement();
//Run a query and store all Result into rs
rs = stmt.executeQuery(sql);
//why not [return stmt.executeQuery(sql);] directly?
}
catch(SQLException ex)
{
//Handle exception
System.err.println("dbtools.executeQuery: " + ex.getMessage());
}
return rs;
}
/************************************************************
* This mathod execute a SQL Update,Insert,Delete?
* The structure for this mathod and above are similar
* except it use executeUpdate instead of executeQuery
*************************************************************/
public void executeUpdate(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
//different from above
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("dbtools.executeSQL: " + ex.getMessage());
}
}
/**************************************************************
* This mathod close all connection if they were opened
* First result set, then
statement. last connection
* <b>Q:Is the order important?</b>
***************************************************************/
public void closeConn()
{
try
{
if (rs!=null)
rs.close();
if (stmt!=null)
stmt.close();
if (conn!=null)
conn.close();
}
catch(SQLException ex) {
System.err.println("dbtools.closeConn: " + ex.getMessage());
}
}
/****************************************************************
* This mathod returns a Hashtable object.
* It's a table with Room-ID/Room-Name information
* Nothing special, perhaps it can use executeQuery mathod declared
* above directly
*****************************************************************/
public Hashtable getRoomList()
{
try
{
// perhaps it can use executeQuery mathod declared bove
// directly instead of the block marked, like this:
// rs = executeQuery)("SELECT id, room from rooms");
//***begin
***
room_list.clear();
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, room from rooms");
//***end***
while (rs.next())
{
room_list.put(rs.getString("id"), rs.getString("room"));
}
closeConn();
}
catch(SQLException ex)
{
System.err.println("dbtools.getWorkDefList: " + ex.getMessage());
}
return room_list;
}
//<b>Q:What's "&amp;nbsp;" ?</b>
/**************************************************************
* This mathod returns all colum name and current value like this
* ColumeA,ValueA
* ColumeB,ValueB
* ...
* The only new things are getMetaData() and getColumnCount()
**************************************************************/
public Hashtable getData(ResultSet ars)
{
col_data.clear();
try
{
for (int ii = 1;
ii <= ars.getMetaData().getColumnCount();
ii++)
{
// Gets the value of a column in the current row
String ss = ars.getString(ii);
ss = ss==null ? "&amp;nbsp;" : ss;
col_data.put(ars.getMetaData().getColumnName(ii), ss);
}
}
catch(SQLException ex)
{
System.err.println("dbtools.getData: " + ex.getMessage());
}
return col_data;
}
}
/*
<b>
Quesstion Review:
Q:Is here test the DB Driver? if so, perhaps
it's not nessary, or it's really nessary?
can I skip this step? if cannot, what these
excatlydo
?
Q:Is the connection is already opened in
the constructor?
Q:Is the order important?
Q:What's "&amp;nbsp;" ?
Q:Any example ussing this dbtool in jsp?:)
</b>
*/
 
Y

yysun

Unregistered / Unconfirmed
GUEST, unregistred user!
Answers:
1.Class.forName is used to load JDBC class, not a test.
http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html
2.The connection is opened when it has to be, not in the constructor.
3.I never tried to change to order since I got this order work.
4." " is a space.
ss = ss==null ? " " : ss;
equals to:
if (ss==null) ss = " ";
else
ss = ss;
5.I have published a jsp with comments which uses this bean.
http://www.delphibbs.com:8080/jsp/
I'm using dbtools in project-m.
http://www.delphibbs.com/project-m
 
E

Emperor

Unregistered / Unconfirmed
GUEST, unregistred user!
i agree yysun
 
C

CJ

Unregistered / Unconfirmed
GUEST, unregistred user!
1. It should be rigister the jdbc driver:)
 
顶部