课内实验报告
课 程 名: 网络编程技术
任课教师: 李宏伟
专 业: 信息系统与信息管理 _
学 号:
姓 名:
二○一一 至 二○一二 年度 第 1 学期
南京邮电大学 经济与管理学院
第二篇:Jsp综合性实验报告
JSP
综合性实验报告
计算机与信息技术学院综合性、设计性实验报告
一、实验目的
本实验旨在学习在JSP技术中怎样使用数据库,包括查询、更新、添加、删除记录等。
二、实验仪器或设备
计算机一台
三、总体设计
设计原理:
在许多Web应用中,服务器需要和用户进行必要的数据交互,例如,服务器需要将用户提供的数据永久、安全地保存在服务器端,需要为用户提供数据查询等,此时,Web应用就可能需要和数据库打交道,其原因是数据库在数据查询、修改、保存、安全等方面有着其他数据处理手段无法替代的地位。本实验就是说明在JSP技术中怎样使用数据库的综合性实验。
四、实验步骤(包括主要步骤、代码分析等)
A、首页面
编写一个首页面,负责与编写好的的随机查询、更新记录、添加记录、删除记录进行链接,点击不同的链接会跳转到不同的页面进行操作。
zonghe.jsp
<%@ page contentType="text/html;charset=GB2312" %>
//page指令用来定义整个JSP页面的一些属性和这些属性的值
<style type="text/css">
body{
background:url(A.jpg) norepeat;
}//添加背景图片
</style>
<HTML><BODY bgcolor=cyan><Font size=3><center>
<br><br><a href="example6_4.jsp">随机查询</a>
<br><br><a href="example6_8.jsp">更新记录</a>
<br><br><a href="example6_9.jsp">添加记录</a>
<br><br> <a href="example6_10.jsp">删除记录</a>
</center>
</FONT></BODY></HTML>
代码分析:
<%@ page contentType="text/html;charset=GB2312" %>
page指令用来定义整个JSP页面的一些属性和这些属性的值,page指令标记可以指定如下属性的值:contentType、import、language、session、buffer、auotFlush、isThreadSafe、pageEncoding。
属性值需用单引号或双引号括起来,但需要注意的是:可以使用多个page指令指定import属性几个值,但其他属性只能使用page指令指定一个值。page指令对整个页面都有效,与其书写的位置无关,但习惯把page指令写在JSP页面的最前面。
language:定义JSP页面使用的脚本语言,该属性值目前只能取JAVA
import:该属性的作用是为JSP页面引入Java核心包中的类
contentType:属性值确定JSP页面响应MIME类型和JSP页面字符编码
session:用于设置是否需要使用内置的session对象
auotFlush:指定out的缓冲区被填满时,缓冲区是否自动刷新isThreadSafe:用来设置JSP页面是否可多线程访问
首页面效果图:
要操作的表:
B、随机查询
example6-4.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><Body bgcolor=cyan><Font size=2>
<P> 随机查询记录.
<FORM action="random.jsp" Method="post" >
输入数据库名: <Input type=text name="databaseName" size=8>
输入表的名字: <Input type=text name="tableName" size=8>
<br>输入用户名:<Input type=text name="user" size=6>(默认是sa)
输入密码:<Input type="password" name="password" size=6>(默认是sa)
<br>输入查询的记录数:<Input type=text name="count" value=4>
<Input type=submit name="g" value="提交">
</FORM>
</Font></Body></HTML>
random.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><Body bgcolor=cyan><Font size=2>
<% String dName=request.getParameter("databaseName");
String tName=request.getParameter("tableName");
String id=request.getParameter("user");
String secret=request.getParameter("password");
String n=request.getParameter("count");
%>
<inquire:RandomQuery
databaseName="<%=dName%>"tableName="<%=tName%>"
user="<%=id%>" password="<%=secret%>" count="<%=n%>"/>
在<%=biao%>表随机查询到<%=randomCount%>条记录:
<BR> <%=queryResult %>
</Font></Body></HTML>
RandomQuery.tag
<%@ tag pageEncoding="utf-8" %>
<%@ tag import="java.sql.*" %>
<%@ tag import="java.util.*" %>
<%@ attribute name="databaseName" required="true" %>
<%@ attribute name="tableName" required="true" %>
<%@ attribute name="user" required="true" %>
<%@ attribute name="password" required="true" %>
<%@ attribute name="count" required="true" %>
<%@ variable name-given="biao" scope="AT_END" %>
<%@ variable name-given="queryResult" scope="AT_END" %>
<%@ variable name-given="randomCount" scope="AT_END" %>
<%
Vector vector=new Vector();
StringBuffer result;
result=new StringBuffer();
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
Connection con;
Statement sql;
ResultSet rs;
int n=0;
try{ result.append("<table border=1>");
String uri="jdbc:odbc:mystar";
con=DriverManager.getConnection(uri,user,password);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,tableName,null);
int 字段个数=0;
result.append("<tr>");
while(rs1.next()){
字段个数++;
String clumnName=rs1.getString(4);
result.append("<td>"+clumnName+"</td>");
}
result.append("</tr>");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM "+tableName);
rs.last();
int rowNumber=rs.getRow();
int number=rowNumber; //获取记录数
for(int i=1;i<=number;i++)
vector.add(new Integer(i));
int m=Math.min(Integer.parseInt(count),number);
n=m;
while(m>0){
int i=(int)(Math.random()*vector.size());
//从vector中随机抽取一个元素:
int index=((Integer)vector.elementAt(i)).intValue();
rs.absolute(index); //游标移到这一行
result.append("<tr>");
for(int k=1;k<=字段个数;k++)
result.append("<td>"+rs.getString(k)+"</td>");
result.append("</tr>");
m--;
vector.removeElementAt(i);
//将抽取过的元素从vector中删除
}
result.append("</table>");
con.close();
}
catch(SQLException e){
result.append("请输入正确的用户名和密码");
}
jspContext.setAttribute("queryResult",new String(result));
//返回queryResult对象
jspContext.setAttribute("biao",tableName);
//返回biao对象
jspContext.setAttribute("randomCount",String.valueOf(n));
//返回randomCount对象
%>
代码分析:
example6-4.jsp
<%@ page contentType="text/html;charset=GB2312" %>
page指令用来定义整个JSP页面的一些属性和这些属性的值,page指令对整个页面都有效,与其书写的位置无关,但习惯把page指令写在JSP页面的最前面。
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
Web服务目录下的一个JSP页面在使用Tag标记来调用一个Tag文件之前,必须首先使用taglib指令标记引入该Web服务目录下的标记库,只有这样,JSP页面才可以使用Tag标记调用相应的Tag文件。一个JSP页面可以使用几个taglib指令标记引入若干个标记库。
<FORM action="random.jsp" Method="post" >
输入数据库名: <Input type=text name="databaseName" size=8>
…… //提交手段
<Input type=submit name="g" value="提交">
</FORM>
用户在请求JSP页面时,通常会使用HTML表单提交信息,其中<FORM>是表单标记,method可取get或post,使用get方法提交的信息会在提交的过程中显示在浏览器的地址栏中,而使用post方法提交的信息不会显示在地址栏中,提交手段包括通过文本框、列表、文本区等。
程序中的表单使用post方法请求random.jsp页面,提交信息的手段是在文本框输入信息,用户单击“提交”按钮请求random.jsp页面,并向random.jsp提交信息。
random.jsp
<Body bgcolor=cyan>
<Font size=2></Font>
</Body>
设置背景的颜色和显示的字号
<% String dName=request.getParameter("databaseName");
String tName=request.getParameter("tableName");
……
%>
random.jsp页面可以使用内置request对象获得用户提交的信息,例如request对象可以使用getParameter(String s)方法获得该表单通过text提交的信息。
RandomQuery.tag
<%@ tag pageEncoding="utf-8" %>
<%@ tag import="java.sql.*" %>
<%@ tag import="java.util.*" %>
Tag文件通过使用tag指令可以指定某些属性的值,以便从总体上影响Tag文件的处理和表示
pageEncoding:该属性的值可以指定Tag文件的字符编码
import:属性的作用是为Tag文件引入Java核心包中的类
<%@ attribute name="databaseName" required="true" %>
在Tag文件中通过使用attribute指令,可以让使用它的JSP页面向该Tag文件传递需要的数据。attribute指令中的name属性是必需的,该属性值是一个对象的名字。JSP页面在调用Tag文件时,可向name属性指定的对象传递一个引用。attribute指令中的required属性默认值是false,当指定required的值是true是,调用该Tag文件的JSP页面必须向该Tag文件中attribute指令中的name属性指定的对象传递一个引用
<%@ variable name-given="biao" scope="AT_END" %>
Tag文件通过使用variable指令可以将Tag文件中的对象返回给调用该Tag文件的JSP页面。variable指令中属性name的值用来指定对象的名字。variable指令中属性scope的值指定对象的有效范围,scope的值可以取AT_BEGIN、NESTEND、AT_END。
AT_BEGIN:JSP页面一旦开始使用Tag标记,就可以使用variable指令中给出的对象
NESTEND:JSP页面只可以在 Tag标记的标记体使用variable指令给出的对象
AT_END:JSP页面只有在Tag标记结束后,才可以使用variable指令中给出的对象
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
建立桥接器:JDBC使用java.lang包中的Class类建立JDBC-ODBC桥接器。Class类通过调用它的静态方法forName加载sun.jdbc.odbc包中的JdbcOdbcDriver类建立JDBC-ODBC桥接器。建立桥接器时可能发生异常,必须捕获这个异常。
Connection con;
try{ String uri="jdbc:odbc:mystar";
con=DriverManager.getConnection(uri,user,password);
}
catch(SQLException e){
result.append("请输入正确的用户名和密码");
}
编写连接数据库代码不会出现数据库的名称,只能出现数据源的名字。首先先使用java.sql包中的Connection类声明一个对象,然后再使用类
DriverManager调用它的静态方法getConnection创建这个连接对象,建立连接时应捕获异常。应用程序一旦和某个数据源建立了连接,就可以通过SQL语句和该数据源所指定的数据库中的表交互信息,如查询、修改、更新表中的记录。
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,tableName,null);
当和数据库建立连接对象con之后,那么该连接对象调用getMetaData()方法可以返回一个DatabaseMetaData对象。
Metadata对象再调用getColumns()方法可以将表的字段信息以行列的形式储存在一个ResultSet对象中。
Statement sql;
ResultSet rs;
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM "+tableName);
有时候需要在结果集中前后移动,显示结果集指定的一条记录或随机显示若干条记录等。这是,必须要返回一个可滚动的结果集。为了得到一个可滚动的结果集,需先获得一个Statement对象,然后,根据参数的取值情况,sql返回相应类型的结果集。
ResultSet.TYPE_SCROLL_SENSITIVE:返回可滚动的结果集,当数据库变化时,当前结果集同步改变
ResultSet.CONCUR_READ_ONLY:不能用结果集更新数据库中的表
SQL查询语句对数据库查询操作将返回一个ResultSet对象,ResultSet对象是以统一形式的列组织的数据行组成,内存的结果集对象rs的列数是4列,刚好和product(表名)的列数相同,第一列至第四列分别是“number”、“name”、“madeTime”、“price”列。
jspContext.setAttribute("queryResult",new String(result));
Tag文件为了给JSP页面返回一个对象,就必须将对象的名字以及该对象的引用存储到Tomcat引擎提供的内置对象jspContext中。Tag文件只有将对象的名字及其引用存储到jspContext中,JSP页面才可以使用该对象。jspContext调用setAttribute("对象的名字",对象的引用)方法储存对象的名字以及该对象的引用。
随机查询:
查询结果:
C、更新记录
example6_8.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><BODY bgcolor=pink ><FONT size=2>
<FORM action="newResult.jsp" method=post>
<table border=1>
<tr><td> 输入要更新的产品的产品号:</td>
<td><Input type="text" name="number"></td></tr>
<tr><td>输入新的名称:</td>
<td><Input type="text" name="name"></td></tr>
<tr><td>输入新的生产日期:</td>
<td><Input type="text" name="madeTime"></td></tr>
<tr><td>输入新的价格:</td>
<td><Input type="text" name="price"></td></tr>
</table>
<BR><Input type="submit" name="b" value="提交更新">
<BR>product表更新前的数据记录是:
<inquire:QueryTag dataSource="mystar" tableName="product"
user="sa" password="sa"/>
<BR> <%=queryResult %>
</Font></BODY></HTML>
newResult.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="renew"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><BODY bgcolor=cyan><Font size=2>
<% String nu=request.getParameter("number");
String na=request.getParameter("name");
String mT=request.getParameter("madeTime");
String pr=request.getParameter("price");
byte bb[]=na.getBytes("iso-8859-1");
na=new String(bb);
%>
<renew:NewRecord number="<%=nu%>" name="<%=na%>"
madeTime="<%=mT%>" price="<%=pr%>"/>
<BR>product表更新后的数据记录是:
<inquire:QueryTag dataSource="mystar" tableName="product"
user="sa" password="sa"/>
<BR> <%=queryResult %>
</Font></BODY></HTML>
QueryTag.tag
<%@ tag pageEncoding="utf-8" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="dataSource" required="true" %>
<%@ attribute name="tableName" required="true" %>
<%@ attribute name="user" required="true" %>
<%@ attribute name="password" required="true" %>
<%@ variable name-given="biao" scope="AT_END" %>
<%@ variable name-given="queryResult" scope="AT_END" %>
<% StringBuffer result;
result=new StringBuffer();
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
Connection con;
Statement sql;
ResultSet rs;
try{ result.append("<table border=1>");
String uri="jdbc:odbc:"+dataSource;
con=DriverManager.getConnection(uri,user,password);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,tableName,
null);
int 字段个数=0;
result.append("<tr>");
while(rs1.next()){
字段个数++;
String clumnName=rs1.getString(4);
result.append("<td>"+clumnName+"</td>");
}
result.append("</tr>");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM "+tableName);
while(rs.next()){
result.append("<tr>");
for(int k=1;k<=字段个数;k++)
result.append("<td>"+rs.getString(k)+"</td>");
result.append("</tr>");
}
result.append("</table>");
con.close();
}
catch(SQLException e){
result.append("请输入正确的用户名和密码");
}
jspContext.setAttribute("queryResult",new String(result));
//返回queryResult对象
jspContext.setAttribute("biao",tableName);
//返回biao对象
%>
NewRecord.tag
<%@ tag pageEncoding="utf-8" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="number" required="true" %>
<%@ attribute name="name" required="true" %>
<%@ attribute name="madeTime" required="true" %>
<%@ attribute name="price" required="true" %>
<% float p=Float.parseFloat(price);
String condition1="UPDATE product SET name= '"+name+
"' WHERE number="+"'"+number+"'",
condition2="UPDATE product SET madeTime= '"+madeTime+
"' WHERE number="+"'"+number+"'",
condition3="UPDATE product SET price= "+price+
" WHERE number="+"'"+number+"'";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
Connection con;
Statement sql;
ResultSet rs;
try{ String uri="jdbc:odbc:mystar";
con=DriverManager.getConnection(uri,"sa","sa");
sql=con.createStatement();
sql.executeUpdate(condition1);
sql.executeUpdate(condition2);
sql.executeUpdate(condition3);
con.close();
}
catch(Exception e){
out.print(""+e);
}.
%>
代码分析:
newResult.jsp
<%@ taglib tagdir="/WEB-INF/tags" prefix="renew"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
一个Tag文件也可以使用几个taglib指令标记引入若干个标记库,其中renew和inquire均为前缀。
QueryTag.tag
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,tableName,null);
当和数据库建立连接对象con之后,那么该连接对象调用getMetaData()
方法可以返回一个DatabaseMetaData对象。
Metadata对象再调用getColumns()方法可以将表的字段信息以行列的形式存储在一个ResultSet对象中。如果product(就是表名)有n个字段,tableMessage(rsl)就刚好有n行、每行4列。每行分别含有和相应字段有关的信息,信息的次序为:“数据库名”、“数据库扩展名”、“表名”、“字段名”。
NewRecord.tag
condition1="UPDATE product SET name= '"+name+"' WHERE
number="+"'"+number+"'",
sql.executeUpdate(condition1);
可以使用SQL语句更新记录中字段的值。Statement对象调用方法:public int executeUpdate(String sqlStatement);通过参数sqlStatement指定的方式实现对数据库表中记录的字段值的更新。
注意:可以使用一个Statement对象进行更新和查询操作,但需要注意的是,当查询语句返回结果集后,如果没有立即输出结果集的记录,而接着执行了更新语句,那么结果集就不能输出记录了。要想输出记录就必须重新返回结果集。
更新记录:
更新结果:
D、添加记录
example6_9.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><BODY bgcolor=pink ><FONT size=2>
<FORM action="newDatabase.jsp" method=post>
添加新记录: <table border=1>
<tr><td> 输入产品号:</td>
<td><Input type="text" name="number"></td></tr>
<tr><td>输入名称:</td>
<td><Input type="text" name="name"></td></tr>
<tr><td>输入生产日期:</td>
<td><Input type="text" name="madeTime"></td></tr>
<tr><td>输入价格:</td>
<td><Input type="text" name="price"></td></tr>
</table>
<BR><Input type="submit" name="b" value="提交">
<BR>product表添加新记录前的记录是:
<inquire:QueryTag dataSource="mystar"tableName="product"
user="sa" password="sa"/>
<BR> <%=queryResult %>
</Font></BODY></HTML>
newDatabase.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><BODY bgcolor=cyan><Font size=2>
<% String nu=request.getParameter("number");
String na=request.getParameter("name");
String mT=request.getParameter("madeTime");
String pr=request.getParameter("price");
byte bb[]=na.getBytes("iso-8859-1");
na=new String(bb);
%>
<inquire:AddRecord number="<%=nu%>" name="<%=na%>"
madeTime="<%=mT%>" price="<%=pr%>"/>
<BR>product表添加新记录后的记录是:
<inquire:QueryTag dataSource="mystar" tableName="product"
user="sa" password="sa"/>
<BR> <%=queryResult %>
</Font></BODY></HTML>
AddRecord.tag
<%@ tag pageEncoding="utf-8" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="number" required="true" %>
<%@ attribute name="name" required="true" %>
<%@ attribute name="madeTime" required="true" %>
<%@ attribute name="price" required="true" %>
<% float p=Float.parseFloat(price);
String condition="INSERT INTO product VALUES"+
"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
Connection con;
Statement sql;
ResultSet rs;
try{ String uri="jdbc:odbc:mystar";
con=DriverManager.getConnection(uri,"sa","sa");
sql=con.createStatement();
sql.executeUpdate(condition);
con.close();
}
catch(Exception e){
out.print(""+e);
}
%>
代码分析:
AddRecord.tag
String condition="INSERT INTO product VALUES"+
"("+"'"+number+"','"+name+"','"+madeTime+"',"+p+")";
sql.executeUpdate(condition1);
可以使用SQL语句添加新的记录。Statement对象调用方法:public int executeUpdate(String sqlStatement);通过参数sqlStatement指定的方式实现对数据库表中添加新的记录。
注意:可以使用一个Statement对象进行添加和查询操作,但需要注意的是,当查询语句返回结果集后,如果没有立即输出结果集的记录,而接着执行了添加语句,那么结果集就不能输出记录了。要想输出记录就必须重新返回结果集。
添加记录:
添加结果:
E、删除记录
example6_10
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><BODY bgcolor=pink ><FONT size=2>
<FORM action="delete.jsp" method=post>
删除记录:<br>输入被删除的记录的产品号:
<Input type="text" name="number" size=8>
<Input type="submit" name="b" value="提交">
<BR>product表删除记录前的记录是:
<inquire:QueryTag dataSource="mystar"tableName="product"
user="sa" password="sa"/>
<BR> <%=queryResult %>
</Font></BODY></HTML>
delete.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="inquire"%>
<HTML><BODY bgcolor=cyan><Font size=2>
<% String nu=request.getParameter("number");%>
<inquire:DelRecord number="<%=nu%>" />
<BR>product表删除记录后的记录是:
<inquire:QueryTag dataSource="mystar"tableName="product"
user="sa" password="sa"/>
<BR> <%=queryResult %>
</Font></BODY></HTML>
DelRecord.tag
<%@ tag pageEncoding="utf-8" %>
<%@ tag import="java.sql.*" %>
<%@ attribute name="number" required="true" %>
<% String condition="DELETE FROM product WHERE number
= '"+number+"'";
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
Connection con;
Statement sql;
ResultSet rs;
try{ String uri="jdbc:odbc:mystar";
con=DriverManager.getConnection(uri,"sa","sa");
sql=con.createStatement();
sql.executeUpdate(condition);
con.close();
}
catch(Exception e){
out.print(""+e);
}
%>
代码分析:
DelRecord.tag
String condition="DELETE FROM product WHERE number
= '"+number+"'";
sql.executeUpdate(condition1);
可以使用SQL语句删除记录。Statement对象调用方法:public int executeUpdate(String sqlStatement);删除数据库表中的记录。
注意:可以使用一个Statement对象进行删除和查询操作,但需要注意的是,当查询语句返回结果集后,如果没有立即输出结果集的记录,而接着执行了删除语句,那么结果集就不能输出记录了。要想输出记录就必须重新返回结果集。
删除记录:
删除结果:
五、结果分析与总结
在许多Web应用中,服务器需要和用户进行必要的数据交互,此时,Web应用就可能需要和数据库打交道,其原因是数据库在数据查询、修改、保存、安全等方面有着其他数据处理手段无法替代的地位。许多优秀的数据库管理系统在数据管理,特别是在基于Web的数据管理方面正在扮演着重要的角色。
JDBC提供了访问数据库的API,即有一些Java类和接口组成,是Java运行平台的核心类库中的一部分。在JSP中可以使用JDBC实现对数据库中表的记录的查询、修改和删除等操作。JDBC技术在数据库开发中占有很重要的地位,JDBC操作不同的数据库仅仅是连接方式上的差异而已,使用JDBC的应用程序一旦和数据库建立连接,就可以使用JDBC提供的API操作数据库。
应用程序为了能和数据库交互信息,必须首先和数据库建立连接,有两种连接方式:建立JDBC-ODBC桥接器和加载Java数据库驱动程序。
使用JDBC-ODBC桥接器访问数据库的3个步骤:
a、建立JDBC-ODBC桥接器
b、创建ODBC数据源
1、创建、修改或删除数据源;
2、为数据源选择驱动程序;
3、数据源名称及所在位置;
4、设置ID与密码;
5、选择数据库;
6、创建数据源
c、和ODBC数据源建立连接
教师签名:
年 月 日