📄 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -