📄 javaapplet_22.html
字号:
<html><!-- #BeginTemplate "/Templates/final.dwt" --> <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>中华网--科技频道--网络教室</title><style type="text/css"><!--.text { font-size: 9pt; line-height: 16pt}.text1 { font-size: 9pt; text-decoration: none ;line-height: 16pt}.large { font-size: 10.5pt;line-height: 16pt}a { text-decoration: underline}a:hover { color: #FF6666; text-decoration: none;}a:visited { color: #336699}td { font-size: 10.5pt; color: #000099}--></style></head><body leftmargin="5" topmargin="5" marginwidth="5" marginheight="5" aLink=#336699 bgColor=#ffffff link=#02029e text=#02029e><table width="760" border="0" cellspacing="0" cellpadding="0" align="center"> <form method="post" action=""> <tr> <td width="189" rowspan="2" valign="top"> <div align="center"><img src="/zh_cn/etc/images/china_logo1.gif" width="180" height="44" alt="china.com"> </div> <table width="80" border="0" cellspacing="5" cellpadding="0" align="center" height="24"> <tr> <td colspan="2" height="2"> <img src="/zh_cn/etc/images/china_logo2.gif" width="137" height="31"></td> </tr> </table> </td> <td colspan="3" rowspan="2" valign="top"> <div align="right"> <table width="464" border="0" cellpadding="0" cellspacing="2"> <tr> <td colspan="3" class="text" valign="bottom" width="20"> <div align="right"><font color=#000000><img src="/zh_cn/etc/images/gif.gif" width="18" height="19" border="0"></font></div> </td> <td width="435" valign="bottom"> <div align="left"><a href="http://chat.china.com/"><span class="text1"><font color="#000000">聊天室</font></span></a><span class="text1"> <font color="#000000">|</font><a href="http://searcher.china.com/" class="text1"><font color="#000000"> 搜索引擎</font></a> <font color="#000000">|</font><a href="http://home4u.china.com/" class="text1"><font color="#000000"> 个人主页</font></a> <font color="#000000">|</font><a href="http://bbs.china.com/" class="text1"><font color="#000000"> 中华论坛</font></a><font color="#000000"> |</font><a href="http://post4u.china.com/" class="text1"><font color="#000000"> 电子杂志</font></a> <font color="#000000">|</font><a href="http://ec.china.com/" class="text1"><font color="#000000"> 电子商务</font></a><font color="#000000"> |</font><a href="http://freemail.china.com" class="text1"><font color="#000000"> 电子邮件</font></a> </span></div> </td> </tr> </table> <span class="text"><!-- BEGIN BANNER CODE--> <script language=javascript>var plugNum = Math.random()+1;document.writeln('<A HREF="http://ap.ads.link4ads.com/bin/wadredir?req=clickthru&siteid=4925&pageid='+plugNum+'" target="_blank"><IMG src="http://ap.ads.link4ads.com/bin/wadredir?req=ad&siteid=4925&pageid='+plugNum+'&mode=via" width=468 height=60 Alt="24/7 Asia ad"></A>');</script> <!-- END BANNER CODE--></span></div> </td> <td rowspan="2" width="102" valign="top" align="center"> <span class="text"> <select name="select3" onChange="parent.location.href=this.options[this.selectedIndex].value"> <option>--特色服务--</option> <option value="http://lovesky.china.com/zh_cn/index.html">缘分的天空</option> <option value="http://app.china.com/medicine/hospital/php/index.php">网上医院</option> <option value="http://stock4u.china.com/">在线炒股</option> <option value="http://job4u.china.com/">网上求职</option> <option value="http://points4u.china.com/">积分大奖</option> <option value="http://game.china.com/zh_cn/game/guigu/index.htm">游戏归谷</option> <option value="http://qf.china.com/">清风围棋</option> <option value="http://wap4u.china.com">手机上网</option> <option value="http://download.china.com/">下载基地</option> <option value="http://desktop.china.com/zh_cn/cool/index.html">桌面壁纸</option> <option value="http://card.china.com/">电子贺卡</option> <option value="http://auto.china.com/zh_cn/">汽车世界</option> </select> </span> <table width="85" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="text"> <div align="center">·<a href="/zh_cn/bbs/index.html" target="_blank">精彩论坛</a><br>·<a href="/zh_cn/chat/index.html" target="_blank">E网情深</a><br>·<a href="/zh_cn/chat/index.html" target="_blank">聊天无限</a> </div> </td> </tr> </table> </td> </tr> <tr> </tr> </form></table><table background="/zh_cn/etc/images/hu.gif" width="676" border="0" cellspacing="0" cellpadding="0" align="center"> <tr valign="bottom"> <td width="52" height="25" > <div align="center"><a href="http://www.china.com/zh_cn/"><font color="#000000" class="text1">主页</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://news.china.com/zh_cn/"><font color="#000000" class="text1">新闻</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://sports.china.com/zh_cn/"><font color="#000000" class="text1">体育</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://game.china.com/zh_cn/"><font color="#000000" class="text1">游戏</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://culture.china.com/zh_cn/"><font class="text1" color="#000000">文化</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://edu.china.com/zh_cn/"><font color="#000000" class="text1">教育</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://health.china.com/zh_cn/"><font color="#000000" class="text1">健康</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://finance.china.com/zh_cn/"><font color="#000000" class="text1">财经</font></a></div> </td> <td height="25" width="52" background="/zh_cn/etc/images/hu1.gif"> <div align="center"><a href="http://tech.china.com/zh_cn/"><font class="text1"><b><font color="#FFFFFF">科技</font></b></font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://travel.china.com/zh_cn/"><font color="#000000" class="text1">旅游</font></a></div> </td> <td height="25" width="52" > <div align="center"><a href="http://military.china.com/zh_cn/"><font color="#000000" class="text1">军事</font></a></div> </td> <td width="52" height="25" > <div align="center"><a href="http://entertainment.china.com/zh_cn/"><font color="#000000" class="text1">娱乐</font></a></div> </td> <td width="52" height="25" > <div align="center"><a href="http://business.china.com/zh_cn/"><font color="#000000" class="text1">商贸</font></a></div> </td> </tr></table> <body bgcolor="#FFFFFF" leftmargin="5" topmargin="5" marginwidth="5" marginheight="5" link="#02029E" vlink="#336699" alink="#336699" text="#02029E"><table width="755" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#626BDF" height="18"> <tr> <td width="50"> </td> <td width="65" class="text"><a href="/zh_cn/news/index.html"><font color="#FFFFFF">科技动态</font></a></td> <td width="65" class="text"><a href="/zh_cn/hardware/index.html"><font color="#FFFFFF">硬件广场</font></a></td> <td width="65" class="text"><a href="http://download.china.com/"><font color="#FFFFFF">下载基地</font></a></td> <td width="65" class="text"><a href="/zh_cn/netschool/index.html"><font color="#FFFF00"><b>网络教室</b></font></a></td> <td width="65" class="text"><a href="/zh_cn/www/index.html"><font color="#FFFFFF">网络冲浪</font></a></td> <td width="65" class="text"><a href="/zh_cn/science/index.html"><font color="#FFFFFF">科学博览</font></a></td> <td width="65" class="text"><a href="/zh_cn/mobile/index.html"><font color="#FFFFFF">移动时代</font></a></td> <td width="65" class="text"><a href="http://wap4u.china.com/"><font color="#FFFFFF">手机上网</font></a></td> <td width="65" class="text"><a href="http://desktop.china.com/"><font color="#FFFFFF">桌面壁纸</font></a></td> <td width="65" class="text"><a href="http://app.china.com/hitech/market/index.php"><font color="#FFFFFF">科技商情</font></a></td> <td width="50"> </td> </tr></table><table width="755" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td colspan="2" width="600" height="212" align="center" valign="top"> <table border=0 width="550" align="center"> <tbody> <tr valign="top"> <td> <center class="large"> <br> <!-SSE START SSE-><!-SSE WIDTH=100% SSE-> </center> <p align="center"><!-- #BeginEditable "%B1%EA%CC%E2" --><font size=4><b>第七天</b></font><!-- #EndEditable --><br> <!-- #BeginEditable "%D7%F7%D5%DF%BC%B0%CA%B1%BC%E4" --> <!-- #EndEditable --></p> <!-- #BeginEditable "%D5%FD%CE%C4" --><span class=large><font color="#000000"> <p align="left">使用Java Applet访问数据库</p> <p> 学习任何的程序语言,当然都得与数据库,Java刚刚诞生的时候,对数据库的支持并不是很好,经过这几年的发展,它对数据库的支持也已经完全达到了成熟的境地。由于这里主要是介绍Java Applet小程序, 因此,不可能用大的篇幅去给大家介绍数据库的知识了,怎么样去建立与设计数据库,还是请大家自己找点书看看吧!这儿,对不住了。</p> <p> 我们这儿以Microsoft Access数据库为例子来说明怎么实现一个数据库的打开与读取的知识。</p> <p>第一步:<br> 使用Microsoft Access创建一个数据库,也就是我的这个例子要用到的,内容就随便了。</p> <p>第二步:也就是编程序了。</p> <p>1)定义变量,变量的定义如下: </p> <p> TextArea theVisits=new TextArea (6,80); <br> //显示数据库的内容 <br> TextField theStatus=new TextField ("",80); <br> //显示打开数据库的信息。相当于程序的状态栏 <br> Connection theConnection; //数据库的连接方法 <br> Statement theStatement; //代表一个发送到数据库来执行的数据库命令 <br> ResultSet theResult; //读取的数据结果,也就是数据库返回的结果 <br> ResultSetMetaData theMetaData; <br> //包含了有关数据库命令执行后 返回结果的有用信息。 <br> String theDataSource; //包含了被访问数据库或者数据源的名称,用URL形式表示 .<br> String theUser; //数据库的用户名 <br> String thePassword; //数据库的密码 <br> 2)实现界面,可以用开始时给大家介绍的方法来实现,界面的实现相对来说,</p> <p>比较的简单了,如下:</p> <p>public void init() <br> {<br> add(theVisits); <br> add(theStatus);<br> theVisits.setEditable (false); //设置文本区域不可以被用户写入<br> theStatus.setEditable (false); //设置文本区域不可以被用户写入<br> openConnection(); //打开数据库的连接<br> execSqlCommand("select * from MyTable"); //从数据库中读取内容<br> closeConnection(); //关闭已经打开的数据库<br> }<br> 3)打开某个数据库的连接</p> <p>public void openConnection()<br> {<br> theDataSource="jdbc:odbc:MyAccess";<br> theUser="";<br> thePassword="";<br> try<br> {<br> Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");<br> theConnection=DriverManager.getConnection(theDataSource,theUser,thePassword);<br> theStatus.setText("Status:OK");<br> }<br> catch (Exception e)<br> {<br> handleException(e);<br> }<br> }</p> <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时,主机和端口信息没有必要提供,因为数据源总是配置在本地主机上。用于ODBC数据源的形式是 </p> <p>jdbc:odbc:<data source name></p> <p> 为了打开数据库连接,用户必须使用具体的驱动程序另外,驱动程序应该指 定如何这样做。如果正在使用JDBC-ODBC桥程序,那么可以通过使用名为Class类 的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对象,用户应该保存这个对象, 因为它是访问数据源的方法。这里,该对象保存到名为theConnection的字段中。</p> <p> 如果在打开连接过程中出现错误,则SQLexception作废,通过在try-catch 块内打开连接的语句,拥护可以处理这种异常。通过使用后面要讲的handleException(), 在TextField内显示一个简短的相应信息。 </p> <p>4)执行SQL命令</p> <p>public void execSQLCommand(String command)<br> {<br> try{<br> theStatement=theConnection.createStatement();<br> theResult=theStatement.executeQuery (command);<br> theMetaData=theResult.getMetaData ();<br> int columnCount=theMetaData.getColumnCount ();<br> theVisits.setText("");<br> while(theResult.next ())<br> {<br> for(int i =1;i<=columnCount;i++)<br> {<br> String colValue=theResult.getString(i);<br> if(colValue==null)colValue="";<br> theVisits.append (colValue+";");<br> }<br> theVisits.append ("\n");<br> }<br> }catch(Exception e)<br> {<br> handleException(e);<br> }<br> }<br> 为了执行该命令,程序使用Connection对象的createStatement()方法创建 一个可以有查询的Statement。然后,它激活Statement对象的executiveQuery() 方法,用来传递包含SQL查询语句的字符串。从init()中传送过来的参数—— Slect * from MyTable是一个简单的数据库查询语言,如果你还不知道什么意思, 那么请你还是查看一本有关数据库的书吧,这些内容很多,不是我这儿一下子能说 清楚的了。它的意思简单的说,就是查找表——MyTable中的所有(*)信息。它会 返回数据库中的所有内容,并把它作为ResultSet访问的结果。接着该程序激活ResultSet 对象的getMetaData()方法。这时,返回一个ResultSetMetaData值,该值存储在 一个称为theMetaData的变量中。使用ResultSetMetaData对象,可以获得很多有用的 数据;这里该程序使用getColumnCount()方法获得结果表中列的数量。最后,该程序反 复使用该结果表,激活theResult的next()方法获得结果表中的每一条记录,只到该方法 使用完全部记录返回flase为止。</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -