🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: cn.bbs.comp.program
1 messages
1 total messages Started by ���ܵ�Сʨ Thu, 07 May 1998 00:00
�����DB2��Java Application��Java Applet
#865
Author: ���ܵ�Сʨ
Date: Thu, 07 May 1998 00:00
141 lines
7504 bytes

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.nju.edu.cn��[FROM: hsia.nju.edu.cn]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads