🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #24105

�����DB2��Java Application��Java Applet (fwd)

#24105
From: ��
Date: Tue, 23 Jun 1998 00:00
139 lines
7315 bytes
[������: pipishi.bbs@bbs.nju.edu.cn (���ܵ�Сʨ), ����: Programming
��  ��: �����DB2��Java Application��Java Applet
����վ: nju_bbs (Thu May  7 21:44:10 1998)
ת��վ: lifebbs!cstshnews!ustcnews!nju_bbs


Java��DB2��ݿ�����(��)

    �����Ϣ��ҵ���޹�˾Ӧ������� ��С��
    ��ͬ��������,����DB2��JDBC֧��Ҳ���Խ�������Ӧ��:Java Ap
plication(Ӧ�ó����Java Applet(СӦ�ó���)��ǰ��ͨ��λ�ڿ�
����,ͨ��CAE(Client Application Enablr), ���ͻ�Ӧ��ʹ������D
B2������ϵ,�Ӷ�ʵ�ֶ���ݿ�ķ��ʡ���������Ҫ�κοͻ���DB2
 ����,��ֻ��Ҫ֧��Java��������ڿͻ��˽���ִ��,���Ƕ���ݿ�
�ķ��ʾͿ������κ�һ�ִ��������ʹ�ܵ�ƽ̨�Ͻ��С�
    ��������������˵��Java Application��Java Applet�ı�̡�
���ȼ���Ҫ���ʵ����Դ��Ϊmydb,JDBC Applet������������TCP/IP
�˿ں�Ϊ2222�Ķ˿���,JDBC���������ڵķ�����Ϊcherry.ncic.ac.
cn,����ϵͳΪAIX 4.1��

    1.�����ʵ���Java��
    ����Java Application,������java.net.URL, jva.sql.*, ibm.
sql.*;
    ����Java Applet,������tempjava.sql.*, ibm.netsql.*��
    2.װ���ʵ���JDBC�����
    ����Java Application,��װ��ibm.sql.DB2Driver;��Ӧ�����
Ϊ:
    Class.forName("ibm.sql.DB2Driver");
    ����Java Applet,��װ��ibm.netsql.DB2Driver;��Ӧ�����Ϊ:
    Class.forName("ibm.netsql.DB2Driver");

    3.����ݿ⽨������
    ͨ��URL����ݿ⽨������,��URL�����ṩ��ݿ�λ�á���Э��
��(subprotocol)������DB2,��Э����Ϊdb2��
    ����Application,��URLΪ:jdbc:db2:mydb�����������Ӷ���
��ʽ����:
    Connection con = DriverManager.getConnection("jdbc:db2:m
ydb");
    ����Applet,��URLΪ:jdbc:db2://cherry.ncic.ac.cn:2222/myd
b�����������Ӷ����ʽ����:
    Connection con=DriverManager.getConnection(jdbc:db2://ch
erry.ncic.ac.cn:222 /mydb,userid, password );
    �������ر�ָ������,����Applet,���ʶ˿ںű������1024��
    4.����ݿ⴫��SQL����,ִ�в�ѯ,�����ؽ��
    ����,�ɴ���һSQL������stmt:
    Statement stmt = con.createStatement
    ִ�в�ѯ,�����ؽ��:
    ResultSet rs = stmt.executeQuery("SQL���"); 
    5.�ر�����
    �����������Ӷ���ر�:
    con.close();
    ��������̿��Կ���Java Applet��Java Application�ı�̻�
������,��Ҫ��ͬ�������JDBC URL�ϡ�����Java Application����
�ݿ��ͨ���ǽ���CAE����ɵ�,��������JDBCU RL��,���ٰ�����
��Ͷ˿ں���һ����,����Щ��Ϣ����Applet��˵���DZ���ġ�
    ��Ȼ,JDBC������ʵ��Ҳ��ͬ,����,����Java Applet��˵,J
DBC�����Ϊibm.nt sql.DB2Driver,�����Java Application,��J
DBC�����Ϊibm.sql.DB2Driver��
    ������һ����������ӳ���,˵����Java Applet�����ͨ��JD
BC����DB2��ݿ⡣����Java Application,ͨ��������������Ŷ���
�������
    Java Applet���ӳ���Դ�����嵥:
    //  Դ�ļ���:MyDB2Applet.java
    //  ����:�������������˵���������JDBC Applet������
���
    //  DB2��ݿ��Java Applet
    import java.net.URL;
    import tempjava.sql.*;
    import ibm.netsql.*;
    import java.awt.Graphics;
    public class DB2Applet extends java.Applet.Applet {
      static {
        try {
         // ��DriverManagerע�������
         Class.forName("ibm.netsql.DB2Driver");
        } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }
    public void init() {
      resize(200,100);
    }
    public void paint(Graphics g) {
      try {
         // JDBC���������ڵĻ���������Ϊ//cherry.ncic.ac.cn
         // JDBC�����������Ķ˿ں�Ϊ2222
         // ���Դ��Ϊmydb
         // ����URL
         String url = "jdbc:db2://cherry.ncic.ac.cn:2222/myd
b";
         // Ҫ���ʵ���ݿ���û�idΪmyid
         String userid = "myid";
         // �������id���Ӧ�Ŀ���Ϊmypassword
         String password = "mypassword";
         Connection con = DriverManager.getConnection(url, u
serid, passwor );
         // ����ݿ�����ȡ���
         Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT * from emp
loyee");
         g.drawString("��ѯ���:", 10, 25);
         // ��ʾ���
         // �������м�¼��ʱ,rs.next()����false
         int y = 50;
         int i = 0;
         while (rs.next() && (i<2)) {
            i++;
            String a= rs.getString(1);
            String str = rs.getString(2);
            String oneLine =  "empno= " + a + "firstname= " 
+ str;
            g.drawString(oneLine, 20, y );
            y = y + 15;
         }
         stmt.close();
         // ������ݿ�
         g.drawString("��ʼ�������...",10, 100);
         stmt = con.createStatement();
         int rowsUpdated = stmt.executeUpdate("UPDATE employ
ee set firstnme =SI LI��where empno = 000010��");
         // ��ʾ�����µļ�¼�е��к�
         String msg = "���µ��� " + rowsUpdated;
           if (1 == rowsUpdated)  msg = msg +" ����";
           else  msg = msg +"����";
         y = y + 40;
         g.drawString(msg, 20, y);
         stmt.close();
         // �ر�����
         con.close();
      }
      catch( Exception e ) {
         e.printStackTrace();
      }
    }
    }

--
�� ��Դ: �й�ƴ�BBSվ [bbs.ustc.edu.cn]
Technical Details
Message-ID: <980623.yoIEB$PBBS@bbs.ustc.edu.cn>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: f6cb0,c37cb038b90fa68,start
X-Google-Attributes: gidf6cb0,public
From: Qin_Qin@bbs.ustc.edu.cn (��)
Subject: �����DB2��Java Application��Java Applet (fwd)
Date: 1998/06/23
Message-ID: <980623.yoIEB$PBBS@bbs.ustc.edu.cn>
X-Deja-AN: 365191722
Organization: �й�ƴ�BBSվ
Newsgroups: cn.bbs.comp.lang.java