网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.3. Building View Components.
.Java中文问题详解.
.在JAVA中连接Oracle数据库.
.Java学习:线程池的简单构建.
.SpringFramework中面向方面编程二.
.Java实例:JAVA实现屏幕抓图 远程.
.JNDI目录服务及LDAO服务器使用入.
.java基础知识:KVM的扩展.
.书评:Java的盛宴(上).
.XML——连接SQL和Web程序的桥梁(.
.用java取得linux系统cpu、内存的.
.Rational Rose 2001安装手册.
.Java小程序源文件的组成.
.软件项目开发之 软件过程RUP初探.
.Java Web 服务学习报告—Web简介.
.Jdk编译Java程序 - applet.
.Java模式设计之单例模式(一).
.用jdk自带包操作XML.
.深入WebLogic Portal UI编程.
.Java违例准则.

JDBC 程序实例

发表日期:2008-1-5



   B.1 Using SELECT import Java.net.URL;
  import java.sql.*;
  class Select {
  public static void main(String argv[]) {
  try {
  // Create a URL specifying an ODBC data source name.
  String url = "jdbc:odbc:wombat";
  // Connect to the database at that URL.
  Connection con = DriverManager.getConnection(url, "kgh", "");
  // Execute a SELECT statement
  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT a, b, c, d, key FROM Table1");
  // Step through the result rows.
  System.out.println("Got results:");
  while (rs.next()) {
  // get the values from the current row:
  int a = rs.getInt(1);
  BigDecimal b = rs.getBigDecimal(2);
  char c[] = rs.getString(3).tocharArray();
  boolean d = rs.getBoolean(4);
  String key = rs.getString(5);
  // Now print out the results:
  System.out.print(" key=" + key);
  System.out.print(" a=" + a);
  System.out.print(" b=" + b);
  System.out.print(" c=");
  for (int i = 0; i < c.length; i++) {
  System.out.print(c[i]);
  }
  System.out.print(" d=" + d);
  System.out.print("\n");
  }
  stmt.close();
  con.close();
  } catch (java.lang.Exception ex) {
  ex.printStackTrace();
  }
  }
  }
  
  B.2 Using UPDATE // Update a couple of rows in a database.
  import java.net.URL;
  import java.sql.*;
  class Update {
  public static void main(String argv[]) {
  try {
  // Create a URL specifying an ODBC data source name.
  String url = "jdbc:odbc:wombat";
  // Connect to the database at that URL.
  Connection con = DriverManager.getConnection(url, "kgh", "");
  // Create a prepared statement to update the "a" field of a
  // row in the "Table1" table.
  // The prepared statement takes two parameters.
  PreparedStatement stmt = con.prepareStatement(
  "UPDATE Table1 SET a = ? WHERE key = ?");
  // First use the prepared statement to update
  // the "count" row to 34.
  stmt.setInt(1, 34);
  stmt.setString(2, "count");
  stmt.executeUpdate();
  System.out.println("Updated \"count\" row OK.");
  // Now use the same prepared statement to update the
  // "mirror" field.
  // We rebind parameter 2, but reuse the other parameter.
  stmt.setString(2, "mirror");
  stmt.executeUpdate();
  System.out.println("Updated \"mirror\" row OK.");
  stmt.close();
  con.close();
  } catch (java.lang.Exception ex) {
  ex.printStackTrace();
  }
  }
  }
上一篇:JDBC性能优化技巧 人气:1032
下一篇:常用JDBC连接方法 人气:1361
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐