|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。数据|数据库packagepkgCDB;
importjava.text.*;
importjava.util.*;
importjava.sql.*;
publicclassclsCDB
{
StringsDBDriver="";
StringsConnStr="";
StringsHint="";
ResultSet rs=null;
Connectionconn=null;
Statement stmt=null;
publicStringgetHint()
{
returnsHint;
}
publicvoidsetDBDriver(Strings)
{
sDBDriver=""+s;
}
publicStringgetDBDriver()
{
returnsDBDriver;
}
publicStringgetConnStr()
{
returnsConnStr;
}
publicvoidsetConnStr(Strings)
{
sConnStr=""+s;
}
publicResultSetgetResultSet()
{
returnrs;
}
//Initializethedatabase
publicbooleanInitialization(StringdbDrive,StringdbConn)
{
sDBDriver=dbDrive;
sConnStr=dbConn;
returnInitialization();
}
publicbooleanInitialization()
{
try
{
Class.forName(sDBDriver);
sHint="Initializationsucessfully";
returntrue;
}
catch(java.lang.ClassNotFoundExceptione){
sHint="Initialization:"+e.getMessage();
returnfalse;
}
}
//Acommonfunctionperhapsusable
publicStringchkStr(StringinStr)
{
StringBufferresult=newStringBuffer("");
charcc;
if(inStr!=null)
{
for(inti=0;i<inStr.length();i++)
{
cc=inStr.charAt(i);
if(cc==)
{
result.append();
}
result.append(cc);
}
}
returnresult.toString();
}
//executeaSQLstatementwithrecordsetreturned
publicResultSetexecuteQuery(Stringsql)
{
rs=null;
try
{
conn=DriverManager.getConnection(sConnStr);
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLExceptionex)
{
sHint="executeQuery:"+ex.getMessage();
}
returnrs;
}
//executeaSQLstatementwithoutreturningrecordset
publicbooleanexecuteUpdate(Stringsql)
{
try
{
conn=DriverManager.getConnection(sConnStr);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
returntrue;
}
catch(SQLExceptionex)
{
sHint="executeUpdate:"+ex.getMessage();
returnfalse;
}
}
//Closeconnections
publicbooleancloseConn()
{
try
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
returntrue;
}
catch(SQLExceptionex)
{
sHint="closeConn:"+ex.getMessage();
returnfalse;
}
}
}
测试用JSP文件
<%@pagelanguage="java"import="java.sql.*" %>
<%@pageimport="pkgCDB.clsCDB"%>
<jsp:useBeanid="CommonDBBean"class="pkgCDB.clsCDB"scope="session"/>
<html>
<head><title>CommonDatabaseBeanTest</title></head>
<body>
<fontsize=4>
<%
if((request.getParameter("DBDriver")==null)
//(request.getParameter("ConnStr")==null)
//(request.getParameter("SQLStr")==null))
{%>
<formmethod=get>
InputDBDriver:<inputtype=textname=DBDrivervalue="sun.jdbc.odbc.JdbcOdbcDriver">
<BR>
InputConnectionString:<inputtype=textname=ConnStrvalue="jdbc:odbc:testBean">
InputSQLQuery:<inputtype=textname=SQLStrvalue="select*fromjobs">
<BR>
<inputtype=submitvalue="Submit">
</form>
<% }
else
{%>
<%if(!CommonDBBean.Initialization(request.getParameter("DBDriver"),request.getParameter("ConnStr")))
{%>
<%=CommonDBBean.getHint()+"<p></p>"%>
<%}
else
{%>
<%ResultSetrs;%>
<%rs=CommonDBBean.executeQuery(request.getParameter("SQLStr"));%>
<%intj=rs.getMetaData().getColumnCount(); %>
<tableBORDER="1">
<tr>
<%for(inti=1;i<=j;++i)
{%>
<td>
<%=rs.getMetaData().getColumnName(i)%>
</td>
<%}%>
</tr>
<%while(rs.next())
{%>
<tr>
<%for(inti=1;i<=j;++i)
{%>
<td>
<%=rs.getString(i)%>
</td>
<%}%>
</tr>
<%}%>
</table>
<%}
}
if(!CommonDBBean.closeConn())
{%>
<%=CommonDBBean.getHint()+"<p></p>"%>
<%}%>
</font>
</body>
</html>
java主要分三块,j2se:java的基础核心语言。j2me:java的微型模块,专门针对内存小,没有持续电源等小型设备。j2ee:java的企业模块,专门针对企业数据库服务器的连接维护。 |
|