📄 1.htm
字号:
<html><!-- #BeginTemplate "/Templates/empolder_doc.dwt" -->
<head>
<!-- #BeginEditable "doctitle" -->
<title>|><| 太平洋电脑信息网 -> 网络学院 -> 开发教室</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
-->
</style>
<link rel="stylesheet" href="/pcedu/style/text.css"></head>
<body bgcolor="#FFFFFF" topmargin=0 leftmargin=0 marginheight="0">
<script language="JavaScript" src="/pcedu/script/top.js">
</script>
<table width=760 border=0 cellspacing=0 cellpadding=0 align=center>
<tr>
<td width=194 height="56"><a href=http://www.pconline.com.cn><img src=http://www.pconline.com.cn/images/pconlinelogo.gif width=162 height=35 vspace=10 border=0></a></td>
<td width=406 height="56">
<script language="JavaScript" src="/pcedu/script/empolder_ad.js">
</script>
</td>
<td width=158 align=right height="56">
<script language="JavaScript" src="/pcedu/script/empolder_ad1.js">
</script>
</td>
<td align=right width=2 height="56"> </td>
</tr>
</table>
<table border=0 cellpadding=0 cellspacing=0 width=760 align="center">
<tbody>
<tr valign=bottom>
<td rowspan=2 width=172><img height=32 src="/pcedu/images/pcedu_lo.gif"
width=172 border="0"></td>
<td height=30 rowspan=2>
<table cellpadding=0 cellspacing=0 width="588" bgcolor="#FFA000" background="/pcedu/images/e_menu5.gif" border="0">
<tbody>
<tr valign="bottom">
<td height="17">
<script language="JavaScript" src="/pcedu/script/title_empolder.js">
</script>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr></tr>
<tr bgcolor="#303880">
<td colspan=2 height=1 valign=bottom><img height=1
src="/pcedu/images/blank.gif" width=1></td>
</tr>
<tr>
<td colspan=2 height=5 valign=bottom><img height=5
src="/pcedu/images/blank.gif" width=1></td>
</tr>
</tbody>
</table>
<table width="760" cellspacing="0" cellpadding="0" align="center" height="37">
<tr>
<td width="170" valign="top">
<table border="0" width="170" height="100%"
cellspacing="1" bgcolor="#000000">
<tr bgcolor="#F8F8D2">
<td width="100%" valign="top"><!-- #BeginEditable "left" -->
<div align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#000000" align="center">
<tr bgcolor="#E17329">
<td height="20" align="center"><font color="#FFFFFF">==<b>开发教室==</b></font></td>
</tr>
</table>
<br>
</div>
<!-- #EndEditable -->
<script language="JavaScript" src="/pcedu/script/left_empolder.js">
</script>
</td>
</tr>
</table>
</td>
<td width="10"><img src="/pcedu/images/blank.gif" width="1" height="1"></td>
<td width="580" valign="top" class="article">
<p><img src="/pcedu/images/666666.gif" width="99%" height="1"></p>
<!-- #BeginEditable "content" -->
<p align="center"><span class="title">Java Applet 入门</span></p>
<p align="right"><a href="mailto:yy435@263.net">yy435</a></p>
<p align="right">太平洋网络学院</p>
<p align="center" class="green">第七天</p>
<p align="center" class="green">使用Java Applet访问数据库</p>
<p> 学习任何的程序语言,当然都得与数据库,Java刚刚诞生的时候,对数据库的支持并不是很好,经过这几年的发展,它对数据库的支持也已经完全达到了成熟的境地。由于这里主要是介绍Java
Applet小程序, 因此,不可能用大的篇幅去给大家介绍数据库的知识了,怎么样去建立与设计数据库,还是请大家自己找点书看看吧!这儿,对不住了。</p>
<p> 我们这儿以Microsoft Access数据库为例子来说明怎么实现一个数据库的打开与读取的知识。</p>
<p>第一步:<br>
使用Microsoft Access创建一个数据库,也就是我的这个例子要用到的,内容就随便了。</p>
<p>第二步:也就是编程序了。</p>
<p>1)定义变量,变量的定义如下:
<pre> TextArea theVisits=new TextArea (6,80);
//显示数据库的内容
TextField theStatus=new TextField ("",80);
//显示打开数据库的信息。相当于程序的状态栏
Connection theConnection; //数据库的连接方法
Statement theStatement; //代表一个发送到数据库来执行的数据库命令
ResultSet theResult; //读取的数据结果,也就是数据库返回的结果
ResultSetMetaData theMetaData;
//包含了有关数据库命令执行后 返回结果的有用信息。
String theDataSource; //包含了被访问数据库或者数据源的名称,用URL形式表示 .
String theUser; //数据库的用户名
String thePassword; //数据库的密码 </pre>
<p>2)实现界面,可以用开始时给大家介绍的方法来实现,界面的实现相对来说,</p>
<p>比较的简单了,如下:</p>
<pre>public void init()
{
add(theVisits);
add(theStatus);
theVisits.setEditable (false); //设置文本区域不可以被用户写入
theStatus.setEditable (false); //设置文本区域不可以被用户写入
openConnection(); //打开数据库的连接
execSqlCommand("select * from MyTable"); //从数据库中读取内容
closeConnection(); //关闭已经打开的数据库
}</pre>
<p> 3)打开某个数据库的连接</p>
<pre>
public void openConnection()
{
theDataSource="jdbc:odbc:MyAccess";
theUser="";
thePassword="";
try
{
Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);
theStatus.setText("Status:OK");
}
catch (Exception e)
{
handleException(e);
}
}
</pre>
<p> 包含数据源名称的字符串格式为一个特殊的语句:</p>
<p> jdbc:<subpotocol>:<subname></p>
<p> 协议 名jdbc必须提供,子协议和子名称根据使用数据源类型而变化。<br>
JDBC DriverManager使用子协议来选择书记源响应的驱动程序。子协议<br>
的通常值为“odbc”和“oracle“,子名称包含了该驱动程序使用的附加<br>
信息。通常情况,该子名称是某个网络名称,例如,用于其它Web服务:</p>
<p>jdba:<subpotocol>://<host.domin><port>/<databasename></p>
<p> 驱动程序的说明应该指定子协议和子名称的正确形式。使用ODBC时,主机<br>
和端口信息没有必要提供,因为数据源总是配置在本地主机上。用于ODBC数据源的形式是 </p>
<p>jdbc:odbc:<data source name></p>
<p> 为了打开数据库连接,用户必须使用具体的驱动程序另外,驱动程序应该指<br>
定如何这样做。如果正在使用JDBC-ODBC桥程序,那么可以通过使用名为Class类<br>
的forName()方法来使用具体的JDBC-ODBC桥驱动程序。</p>
<p>class.forName("sun.jdbc.JdbcOdbcDriver");</p>
<p> 然后,拥护可以告诉rManager打开数据源的连接,使用下列方法:</p>
<p>theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);</p>
<p> getConnection()方法返回一个Connection对象,用户应该保存这个对象,<br>
因为它是访问数据源的方法。这里,该对象保存到名为theConnection的字段中。</p>
<p> 如果在打开连接过程中出现错误,则SQLexception作废,通过在try-catch<br>
块内打开连接的语句,拥护可以处理这种异常。通过使用后面要讲的handleException(),<br>
在TextField内显示一个简短的相应信息。 </p>
<p>4)执行SQL命令</p>
<pre>public void execSQLCommand(String command)
{
try{
theStatement=theConnection.createStatement();
theResult=theStatement.executeQuery (command);
theMetaData=theResult.getMetaData ();
int columnCount=theMetaData.getColumnCount ();
theVisits.setText("");
while(theResult.next ())
{
for(int i =1;i<=columnCount;i++)
{
String colValue=theResult.getString(i);
if(colValue==null)colValue="";
theVisits.append (colValue+";");
}
theVisits.append ("\n");
}
}catch(Exception e)
{
handleException(e);
}
}</pre>
<p> 为了执行该命令,程序使用Connection对象的createStatement()方法创建<br>
一个可以有查询的Statement。然后,它激活Statement对象的executiveQuery()<br>
方法,用来传递包含SQL查询语句的字符串。从init()中传送过来的参数——<br>
Slect * from MyTable是一个简单的数据库查询语言,如果你还不知道什么意思,<br>
那么请你还是查看一本有关数据库的书吧,这些内容很多,不是我这儿一下子能说<br>
清楚的了。它的意思简单的说,就是查找表——MyTable中的所有(*)信息。它会<br>
返回数据库中的所有内容,并把它作为ResultSet访问的结果。接着该程序激活ResultSet<br>
对象的getMetaData()方法。这时,返回一个ResultSetMetaData值,该值存储在<br>
一个称为theMetaData的变量中。使用ResultSetMetaData对象,可以获得很多有用的<br>
数据;这里该程序使用getColumnCount()方法获得结果表中列的数量。最后,该程序反<br>
复使用该结果表,激活theResult的next()方法获得结果表中的每一条记录,只到该方法<br>
使用完全部记录返回flase为止。</p>
<p>5)关闭数据库连接</p>
<pre>public void closeConnection()
{
try{
theConnection.close ();
}
catch(Exception e)
{
handleException(e);
}
}</pre>
<p> 当用户访问完某个数据库时,应该关闭数据库连接,释放与连接有关的资源。用户创建<br>
的任何打开的ResultSet或者Statement对象将 自动关闭。另外,在关闭连接时可能发<br>
生一个SQLException,也放到后面处理。</p>
<p>6)处理异常和错误</p>
<pre>public void handleException(Exception e)
{
theStatus.setText("Error:"+e.getMessage ());
e.printStackTrace ();
if(e instanceof SQLException)
{
while((e=((SQLException)e).getNextException ())!=null)
{
System.out.println(e);
}
}
}</pre>
<p> handleException()方法用来处理以上的错误和异常。该方法设置状态TextField包含与<br>
异常有关的错误消息,使用getMessage()方法获得该消息。它还可以在System.out打印<br>
堆栈轨迹。 </p>
<p>三 安装ODBC数据源</p>
<p>为了运行这个应用程序,用户必须 首先创建一个与MyTable数据库相对应的 ODBC数据源。<br>
为了通过ODBC使用Access,用户必须安装Access Odbc驱动程序。如果用户没有安装该驱动<br>
程序,那么还是先安装吧,什么,不会,不会也不行了,找一本书好好看看吧,这是肯定要<br>
看的。我这儿简单的给你说一下吧:</p>
<p>1.从控制面板中选择ODBC-32,双击(要是没有这个图标,那我真的没有办法了,它在安装<br>
数据库的时候,应该会安装上的,否则就只能找一张数据库系统的安装盘,找到ODBC驱动程<br>
序进行安装)。</p>
<p>2.单击添加,选择Microsoft Access Driver。(如果这个图标也没有,那么你的机器是严<br>
重缺乏营养,赶紧装数据库必须的软件,先装Access,这儿要用)</p>
<p>3.输入你要使用的数据源的名称(如果没有,那就得先创建,我这儿就不说Access的使用方<br>
法了)。描述就随便你了。</p>
<p>4.单击选择按钮选择你已经创建好了的Access数据库的文件。</p>
<p>5.点确定就可以了。如果你想为你数据库保密的话,可以加上用户名和密码。(在高级选项里面)。</p>
<p> 要说明一点的是,如果你用VJ++编辑的此程序,要是运行不了,那就是数据库的连接没有设<br>
置好,怎么将数据库设计好,以后再说吧。另外,VJ++有它自己的数据库驱动程序。很适合与<br>
VJ++一起使用,可惜介绍这方面的书好象不是很多。</p>
<p align="center"><a href="../06/13.htm">[上一页]</a></p>
<!-- #EndEditable -->
<table width="100%" border="0">
<tr class="sfont">
<td>
<div align="center"><a href="javascript:history.go(-1)">[返回]</a>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<hr noshade size="2" width="760">
<div align="center">
<p align="center"><font color="#000000">
<script language="JavaScript" src="/pcedu/script/title_edu.js">
</script>
</font> <br>
<br>
版权所有©2000 太平洋电脑网<br>
<font face="Arial, Helvetica, sans-serif">
<script>
document.write("<a href=http://best.netease.com/cgi-bin/view/viewbasic.cgi?exp target=_blank><img src=http://best.netease.com/cgi-bin/log.cgi?user=exp&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" border=0 alt='网易中文排行榜' width=1 height=1></a>");
</script>
<a href=mailto:webmaster@pconline.com.cn> </a></font><font face="Arial, Helvetica, sans-serif"><a href=mailto:webmaster@pconline.com.cn>webmaster@pconline.com.cn</a></font><font face="Arial, Helvetica, sans-serif"><a href=mailto:webmaster@pconline.com.cn>
<script language="">document.write("<a href=http://best.netease.com/cgi-bin/view/viewbasic.cgi?pconline1 target=_blank><img src=http://best.netease.com/cgi-bin/log.cgi?user=pconline1&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" border=0 width=1 height=1 ></a>");</script>
</a>
<script language="">
document.write("<a href=http://count.pconline.com.cn/admin/index.php target=_blank><img src=http://count.pconline.com.cn/count.php?user=pcedu&refer="+escape(document.referrer)+" border=0 width=0 height=0 alt='' ></a>");
</script>
</font> </p>
</div>
</body>
<!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -