免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2153 | 回复: 4
打印 上一主题 下一主题

[java]制作中的跨数据库连接器。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-09-25 20:53 |只看该作者 |倒序浏览
做大作业时想到的,设计方案中能够访问绝大多数关系数据库。
暂时保存。不少地方还有错误,也还没加图形界面。
但是在设计方案中,做好后它能同时适用于Console、X-Window和MS-Window$。

源文件如下:


带主方法的“test_platform.java”
  1. import java.io.*;
  2. public class test_platform
  3. {
  4.         public static void main(String args[])
  5.         {
  6.                 String string_1="[main(String args[]) start]";
  7.                 String string_2="[main(String args[]) ended]";
  8.                 System.out.println(string_1);
  9.                 try
  10.                 {
  11.                         Test_1 A=new Test_1();//调用其他类中的非构造方法,要先创建这个类的对象
  12.                         A.test_1();//通过该类调用此方法
  13.                 }
  14.                 catch(Exception e)
  15.                 {
  16. System.out.println(e);
  17.                 }
  18.                 System.out.println(string_2);
  19.         }
  20. }
复制代码

调用连接器“Test_1.java”
  1. //0.5.8-80
  2. import java.io.*;
  3. import java.sql.*;
  4. public class Test_1
  5. {
  6.         public void test_1()
  7.         {
  8.                 String conffile="/usr2/home/root/work/java/tmp/db.conf";
  9.                 int mode=0;
  10.                 char LG='E';
  11.                 int GUI=0;
  12.                 int failed;
  13.                 System.out.println("[test_1() start]");
  14.                 Connection connect=null;
  15.                 Statement C=null;
  16.                 ResultSet D=null;
  17.                 //String sql_1="create table tbtest_3(employee_number int,surname varchar(40));";
  18.                 String sql_1="create table tbtest_5(opt_int int,opt_varchar40 varchar(40),opt_tinyint128 tinyint);";
  19.                 DB_init B=new DB_init();
  20.                 failed=B.db_init(conffile,mode,LG,GUI);
  21.                 connect=B.db_result();
  22.                 System.out.println("\t"+failed);
  23. /*
  24.                 try
  25.                 {
  26.                         C=connect.createStatement();
  27.                         C.execute(sql_1);
  28.                         //D=C.executeQuery();
  29.                 }
  30.                 catch(SQLException e)
  31.                 {
  32. System.out.println("[[["+e+"]]]");
  33.                 }
  34. */
  35.                 System.out.println("[test_1() ended]");
  36.         }
  37. }
复制代码

jdbc连接器“DB_init.java”
  1. //0.5.8-80
  2. //LG:language
  3. //org.gjt.mm.mysql.Driver
  4. //jdbc:mysql://192.168.1.7/dbtest_1
  5. //String sql_1="create table tbtest_x(opt_int int,opt_varchar40 varchar(40),opt_tinyint128 tinyint);";
  6. import java.io.*;
  7. import java.sql.*;
  8. public class DB_init
  9. {
  10.         public static Connection connect=null;
  11.         public static int failed=0;
  12.         private static String DBMS_name="MySQL";
  13.         private static String driver="org.gjt.mm.mysql.Driver";
  14.         private static String url="jdbc:mysql://192.168.1.1/dbtest_1";
  15.         private static String login="";
  16.         private static String passwd="";
  17.         public static int db_init(String conffile,int mode,char LG,int GUI)
  18.         {
  19.                 System.out.println("[db_init(char LG,int GUI) start]");
  20. System.out.println("\t"+conffile+"\t"+mode+"\t"+LG+"\t"+GUI);
  21.                 failed=1;
  22.                 load_conf(conffile,mode);
  23.                 if(mode==0)
  24.                 {
  25.                         db_manual(conffile,LG,GUI);
  26.                 }
  27.                 else if(mode==1||mode==2)
  28.                 {
  29.                         db_connect(LG,GUI,mode);
  30.                 }
  31.                 System.out.println("[db_init(int mode,char LG,int GUI) ended]");
  32.                 System.out.println(DBMS_name);
  33.                 System.out.println(driver);
  34.                 System.out.println(url);
  35.                 System.out.println(login);
  36.                 System.out.println(passwd);
  37. System.out.println("\t"+conffile+"\t"+mode+"\t"+LG+"\t"+GUI);
  38.                 System.out.println("[db_init(char LG,int GUI) ended]");
  39.                 return failed;
  40.         }
  41.         public static Connection db_result()
  42.         {
  43.                 return connect;
  44.         }
  45.         public static void screen_up()
  46.         {
  47.                 System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  48.         }
  49.         private static void load_conf(String conffile,int mode)
  50.         {
  51.                 String dbconf="";
  52.                 char tmp_0[]=new char[0];
  53.                 char tmp_1[]=new char[0];
  54.                 char tmp_2[]=new char[0];
  55.                 char tmp_3[]=new char[0];
  56.                 char tmp_4[]=new char[0];
  57.                 char tmp_5[]=new char[0];
  58.                 if(conffile!="")
  59.                 {
  60.                         try
  61.                         {
  62.                                 Stringld loader=new Stringld();
  63.                                 loader.stringld(conffile);
  64.                                 dbconf=loader.up_send();
  65.                         }
  66.                         catch(Exception e_1)
  67.                         {
  68.                                 dbconf="";
  69.                                 if(mode==1||mode==2)
  70.                                 {
  71.                                         System.out.println(e_1);
  72.                                 }
  73.                         }
  74.                 }
  75.                 tmp_0=dbconf.toCharArray();
  76. System.out.print("dbconf:\n"+dbconf+"\n");
  77. System.out.print("dbconf:\n"+tmp_0+"\n");
  78.         }
  79.         private static String save_conf(String conffile,char LG,int GUI)
  80.         {
  81.                 String dbconf;
  82.                 InputStreamReader isr_1;
  83.                 BufferedReader br_1;
  84.                 String tmp="";
  85.                 char c1='\0';
  86.                 if(GUI==0)
  87.                 {
  88.                         screen_up();
  89.                         System.out.print(conffile+"\n\n");
  90.                         if(conffile=="")
  91.                         {
  92.                                 if(LG=='C')
  93.                                 {
  94.                                         System.out.print("当前连接将被保存进一个文件。为这个文件命\n");
  95.                                         System.out.print("名,请先输入文件名,然后按“回车”键:\n\n");
  96.                                 }
  97.                                 else
  98.                                 {
  99.                                         System.out.print("Current connection will be save into a\n");
  100.                                         System.out.print("file. To name this file, firstly input\n");
  101.                                         System.out.print("the name, then press key<Enter>;:\n\n");
  102.                                 }
  103.                         }
  104.                         else
  105.                         {
  106.                                 if(LG=='C')
  107.                                 {
  108.                                         System.out.print("直接按“回车”键,当前连接将被保存到这个\n");
  109.                                         System.out.print("文件,它里面原有的记录将被冲掉;\n\n");
  110.                                         System.out.print("如果不想冲掉,请输入一个新文件名,然后按\n");
  111.                                         System.out.print("“回车”键:\n\n");
  112.                                 }
  113.                                 else
  114.                                 {
  115.                                         System.out.print("Directly press key<Enter>; to save current\n");
  116.                                         System.out.print("connection into this file and broom the\n");
  117.                                         System.out.print("old note;\n\n");
  118.                                         System.out.print("Else you can input a new name and press\n");
  119.                                         System.out.print("key<Enter>;.\n\n");
  120.                                 }
  121.                         }
  122.                         try
  123.                         {
  124.                                 isr_1=new InputStreamReader(System.in);
  125.                                 br_1=new BufferedReader(isr_1);
  126.                                 tmp=br_1.readLine();
  127.                         }
  128.                         catch(IOException e_1)
  129.                         {
  130.                                 System.out.println(e_1);
  131.                         }
  132.                         if(!tmp.equals(""))
  133.                         {
  134.                                 conffile=tmp;
  135.                         }
  136.                 }
  137.                 else
  138.                 {
  139.                         //use JFileChooser
  140.                 }
  141.                 dbconf="[DBMS_name]"+DBMS_name+"\n[driver]"+driver+"\n[url]"+url+"\n[login]"+login+"\n[passwd]"+passwd+"\n";
  142.                 try
  143.                 {
  144.                         Stringsv saver=new Stringsv();
  145.                         saver.stringsv(conffile,dbconf);
  146.                 }
  147.                 catch(Exception e_1)
  148.                 {
  149.                         if(GUI==0)
  150.                         {
  151.                                 screen_up();
  152.                                 System.out.println(e_1);
  153.                                 do
  154.                                 {
  155.                                         try
  156.                                         {
  157.                                                 c1=(char)System.in.read();
  158. //System.out.println("c1="+c1);
  159.                                         }
  160.                                         catch(IOException e_2)
  161.                                         {
  162.                                                 System.out.println(e_2);
  163.                                         }
  164.                                 }
  165.                                 while(c1!='\n');
  166.                         }
  167.                         else
  168.                         {
  169.                         }
  170.                 }
  171. System.out.println("dbconf:\n"+dbconf+"\n");
  172.                 return conffile;
  173.         }
  174.         private static void db_manual(String conffile,char LG,int GUI)
  175.         {
  176.                 //>;>;For StringInput
  177.                 InputStreamReader isr_1;
  178.                 String input_1;
  179.                 BufferedReader br_1;
  180.                 /*
  181.                 try
  182.                 {
  183.                         isr_1=new InputStreamReader(System.in);
  184.                         br_1=new BufferedReader(isr_1);
  185.                         input_1=br_1.readLine();
  186. System.out.println("input_1:\n\t"+input_1);
  187.                 }
  188.                 catch(IOException e_1)
  189.                 {
  190.                         System.out.println(e_1);
  191.                 }
  192.                 */
  193.                 //<<For StringInput
  194.                 //>;>;Model for one charater input
  195.                 /*
  196.                 try
  197.                 {
  198.                         c1=(char)System.in.read();
  199. System.out.println("c1="+c1);
  200.                 }
  201.                 catch(IOException e_1)
  202.                 {
  203.                         System.out.println(e_1);
  204.                 }
  205.                 */
  206.                 //<<Model for one charater input
  207.                 int mode=0;
  208.                 //>;>;For temp
  209.                 String tmp;
  210.                 char c1,c2,c3;
  211.                 int i1,i2,i3,i4;
  212.                 //<<For temp
  213.                 do
  214.                 {
  215.                         failed=1;
  216.                         c1=c2='\0';
  217.                         if(GUI==0)
  218.                         {
  219.                                 screen_up();
  220.                                 //>;>;Selections
  221.                                 if(LG=='C')
  222.                                 {
  223.                                         System.out.print("以下项目中,你打算开始哪一个?请先输入它\n");
  224.                                         System.out.print("的序号,然后按“回车”键。\n\n");
  225.                                         System.out.print("              [Z: 关  于 ]\n\n");
  226.                                         System.out.print("      [1: 查  看  当  前  连  接 ]\n\n");
  227.                                         System.out.print("      [2: 保  存  当  前  连  接 ]\n\n");
  228.                                         System.out.print("      [3: 使  用  当  前  连  接 ]\n\n");
  229.                                         System.out.print("      [0: 放  弃  这  次  连  接 ]\n\n");
  230.                                 }
  231.                                 else
  232.                                 {
  233.                                         System.out.print("Which would you like to choose? Type its\n");
  234.                                         System.out.print("number and then press key<Enyer>;.\n\n");
  235.                                         System.out.print("              [Z: ABOUT]\n\n");
  236.                                         System.out.print("      [1: View current connection]\n\n");
  237.                                         System.out.print("      [2: Save current connection]\n\n");
  238.                                         System.out.print("      [3: Adapt  this  connection]\n\n");
  239.                                         System.out.print("      [0: Abandon this connection]\n\n");
  240.                                 }
  241.                                 //<<Selections
  242.                                 //>;>;Choose
  243.                                 try
  244.                                 {
  245.                                         c1=(char)System.in.read();
  246.                                 }
  247.                                 catch(IOException e_1)
  248.                                 {
  249.                                         System.out.println(e_1);
  250.                                 }
  251.                                 //<<Choose
  252.                                 //>;>;Eat for key<Enter>;
  253.                                 c2='\0';
  254.                                 while(c1!='\n'&&c2!='\n')
  255.                                 {
  256.                                         try
  257.                                         {
  258.                                                 c2=(char)System.in.read();
  259.                                         }
  260.                                         catch(IOException e_1)
  261.                                         {
  262.                                                 System.out.println(e_1);
  263.                                         }
  264.                                 }
  265.                                 //<<Eat for key<Enter>;
  266.                         }
  267.                         else if(GUI==1)
  268.                         {
  269.                         }
  270. //System.out.println("c1="+c1);
  271.                         if(c1=='Z'||c1=='z')
  272.                         {
  273.                                 if(GUI==0)
  274.                                 {
  275.                                         screen_up();
  276.                                         if(LG=='C')
  277.                                         {
  278.                                                 System.out.print("《TFW-数据库连接器(Java)》\n");
  279.                                                 System.out.print("版本:0.5.8-80\n");
  280.                                                 System.out.print("========================================\n");
  281.                                                 System.out.print("模型:\n");
  282.                                                 System.out.print("\t自由狼-台风\n");
  283.                                                 System.out.print("\t杨文亮(教员)\n");
  284.                                                 System.out.print("代码:\n");
  285.                                                 System.out.print("\t自由狼-台风\n");
  286.                                                 System.out.print("\t祁可闻\n");
  287.                                                 System.out.print("测试:\n");
  288.                                                 System.out.print("\t自由狼-台风\n");
  289.                                                 System.out.print("\t祁可闻\n");
  290.                                                 System.out.print("版权释放:\n");
  291.                                                 System.out.print("\t自由狼-台风\n");
  292.                                                 System.out.print("========================================\n");
  293.                                                 System.out.print("按“回车”键返回。");
  294.                                         }
  295.                                         else
  296.                                         {
  297.                                                 System.out.print("\"TFW-DATABASE-CONNECTOR(Java)\"\n");
  298.                                                 System.out.print("Version: 0.5.8-80\n");
  299.                                                 System.out.print("========================================\n");
  300.                                                 System.out.print("Model by:\n");
  301.                                                 System.out.print("\tTyphoon.Free.Wolf\n");
  302.                                                 System.out.print("\tYangWenliang(Teacher)\n");
  303.                                                 System.out.print("Code by:\n");
  304.                                                 System.out.print("\tTyphoon.Free.Wolf\n");
  305.                                                 System.out.print("\tQiKewen\n");
  306.                                                 System.out.print("Test by:\n");
  307.                                                 System.out.print("\tTyphoon.Free.Wolf\n");
  308.                                                 System.out.print("\tQiKewen\n");
  309.                                                 System.out.print("Copyleft by:\n");
  310.                                                 System.out.print("\tTyphoon.Free.Wolf\n");
  311.                                                 System.out.print("========================================\n");
  312.                                                 System.out.print("Press key<Enter>; to go back.");
  313.                                         }
  314.                                         //>;>;Wait
  315.                                         do
  316.                                         {
  317.                                                 try
  318.                                                 {
  319.                                                         c1=(char)System.in.read();
  320.                                                 }
  321.                                                 catch(IOException e_1)
  322.                                                 {
  323.                                                         System.out.println(e_1);
  324.                                                 }
  325.                                         }
  326.                                         while(c1!='\n');
  327.                                         //<<Wait
  328.                                 }
  329.                                 else if(GUI==1)
  330.                                 {
  331.                                 }
  332.                         }
  333.                         else if(c1=='1')
  334.                         {
  335.                                 //>;>;Set connection
  336.                                 if(GUI==0)
  337.                                 {
  338.                                         do
  339.                                         {
  340.                                                 screen_up();
  341.                                                 //>;>;Selections
  342.                                                 if(LG=='C')
  343.                                                 {
  344.                                                         System.out.print("以下项目中,你打算开始哪一个?请先输入它\n");
  345.                                                         System.out.print("的序号,然后按“回车”键。\n\n");
  346.                                                         System.out.print("[1:测试当前连接]\n");
  347.                                                         System.out.print("[2:设定这个]当前数据库管理系统:"+DBMS_name+"\n");
  348.                                                         System.out.print("[3:设定这个]当前的jdbc驱动:"+driver+"\n");
  349.                                                         System.out.print("[4:设定这个]当前数据库的URL:"+url+"\n");
  350.                                                         System.out.print("[5:设定这个]当前数据库登录用户:"+login+"\n");
  351.                                                         System.out.print("[6:设定这个]当前数据库用户口令:"+passwd+"\n");
  352.                                                         System.out.print("[0: 什么都不做 ]\n");
  353.                                                         System.out.print("\n");
  354.                                                 }
  355.                                                 else
  356.                                                 {
  357.                                                         System.out.print("Which would you like to choose? Type its\n");
  358.                                                         System.out.print("number and then press key<Enyer>;.\n\n");
  359.                                                         System.out.print("[1:Test current connection]\n");
  360.                                                         System.out.print("[2:Set it]Current DatabaseManageSystem:"+DBMS_name+"\n");
  361.                                                         System.out.print("[3:Set it]Current \"jdbc-driver\":"+driver+"\n");
  362.                                                         System.out.print("[4:Set it]Current URL of the database:"+url+"\n");
  363.                                                         System.out.print("[5:Set it]Current \"login\" name of the\ndatabase:"+login+"\n");
  364.                                                         System.out.print("[6:Set it]Current \"login\" password of\nthe database:"+passwd+"\n");
  365.                                                         System.out.print("[0: Do nothing ]\n");
  366.                                                         System.out.print("\n");
  367.                                                 }
  368.                                                 //<<Selections
  369.                                                 //>;>;Choose
  370.                                                 try
  371.                                                 {
  372.                                                         c1=(char)System.in.read();
  373.                                                 }
  374.                                                 catch(IOException e_1)
  375.                                                 {
  376.                                                         System.out.println(e_1);
  377.                                                 }
  378.                                                 //<<Choose
  379.                                                 //>;>;Eat for key<Enter>;
  380.                                                 c2='\0';
  381.                                                 while(c1!='\n'&&c2!='\n')
  382.                                                 {
  383.                                                         try
  384.                                                         {
  385.                                                                 c2=(char)System.in.read();
  386.                                                         }
  387.                                                         catch(IOException e_1)
  388.                                                         {
  389.                                                                 System.out.println(e_1);
  390.                                                         }
  391.                                                 }
  392.                                                 //<<Eat for key<Enter>;
  393.                                                 if(c1=='1')
  394.                                                 {
  395.                                                         con_test(LG,GUI);
  396.                                                 }
  397.                                                 else if(c1=='2')
  398.                                                 {
  399.                                                         screen_up();
  400.                                                         System.out.print(DBMS_name+"\n\n");
  401.                                                         if(LG=='C')
  402.                                                         {
  403.                                                                 System.out.print("输入,然后按“回车”键:\n\n\n");
  404.                                                         }
  405.                                                         else
  406.                                                         {
  407.                                                                 System.out.print("Input, the press key<Enter>;:\n\n\n");
  408.                                                         }
  409.                                                         try
  410.                                                         {
  411.                                                                 isr_1=new InputStreamReader(System.in);
  412.                                                                 br_1=new BufferedReader(isr_1);
  413.                                                                 tmp=br_1.readLine();
  414.                                                                 if(!tmp.equals(""))
  415.                                                                 {
  416.                                                                         DBMS_name=tmp;
  417.                                                                 }
  418.                                                         }
  419.                                                         catch(IOException e_1)
  420.                                                         {
  421.                                                                 System.out.println(e_1);
  422.                                                         }
  423.                                                 }
  424.                                                 else if(c1=='3')
  425.                                                 {
  426.                                                         screen_up();
  427.                                                         System.out.print(driver+"\n\n");
  428.                                                         if(LG=='C')
  429.                                                         {
  430.                                                                 System.out.print("输入,然后按“回车”键:\n\n\n");
  431.                                                         }
  432.                                                         else
  433.                                                         {
  434.                                                                 System.out.print("Input, the press key<Enter>;:\n\n\n");
  435.                                                         }
  436.                                                         try
  437.                                                         {
  438.                                                                 isr_1=new InputStreamReader(System.in);
  439.                                                                 br_1=new BufferedReader(isr_1);
  440.                                                                 tmp=br_1.readLine();
  441.                                                                 if(!tmp.equals(""))
  442.                                                                 {
  443.                                                                         driver=tmp;
  444.                                                                 }
  445.                                                         }
  446.                                                         catch(IOException e_1)
  447.                                                         {
  448.                                                                 System.out.println(e_1);
  449.                                                         }
  450.                                                 }
  451.                                                 else if(c1=='4')
  452.                                                 {
  453.                                                         screen_up();
  454.                                                         System.out.print(url+"\n\n");
  455.                                                         if(LG=='C')
  456.                                                         {
  457.                                                                 System.out.print("输入,然后按“回车”键:\n\n\n");
  458.                                                         }
  459.                                                         else
  460.                                                         {
  461.                                                                 System.out.print("Input, the press key<Enter>;:\n\n\n");
  462.                                                         }
  463.                                                         try
  464.                                                         {
  465.                                                                 isr_1=new InputStreamReader(System.in);
  466.                                                                 br_1=new BufferedReader(isr_1);
  467.                                                                 tmp=br_1.readLine();
  468.                                                                 if(!tmp.equals(""))
  469.                                                                 {
  470.                                                                         url=tmp;
  471.                                                                 }
  472.                                                         }
  473.                                                         catch(IOException e_1)
  474.                                                         {
  475.                                                                 System.out.println(e_1);
  476.                                                         }
  477.                                                 }
  478.                                                 else if(c1=='5')
  479.                                                 {
  480.                                                         screen_up();
  481.                                                         System.out.print(login+"\n\n");
  482.                                                         if(LG=='C')
  483.                                                         {
  484.                                                                 System.out.print("输入,然后按“回车”键:\n\n\n");
  485.                                                         }
  486.                                                         else
  487.                                                         {
  488.                                                                 System.out.print("Input, the press key<Enter>;:\n\n\n");
  489.                                                         }
  490.                                                         try
  491.                                                         {
  492.                                                                 isr_1=new InputStreamReader(System.in);
  493.                                                                 br_1=new BufferedReader(isr_1);
  494.                                                                 tmp=br_1.readLine();
  495.                                                                 if(!tmp.equals(""))
  496.                                                                 {
  497.                                                                         login=tmp;
  498.                                                                 }
  499.                                                         }
  500.                                                         catch(IOException e_1)
  501.                                                         {
  502.                                                                 System.out.println(e_1);
  503.                                                         }
  504.                                                 }
  505.                                                 else if(c1=='6')
  506.                                                 {
  507.                                                         screen_up();
  508.                                                         System.out.print(passwd+"\n\n");
  509.                                                         if(LG=='C')
  510.                                                         {
  511.                                                                 System.out.print("输入,然后按“回车”键:\n\n\n");
  512.                                                         }
  513.                                                         else
  514.                                                         {
  515.                                                                 System.out.print("Input, the press key<Enter>;:\n\n\n");
  516.                                                         }
  517.                                                         try
  518.                                                         {
  519.                                                                 isr_1=new InputStreamReader(System.in);
  520.                                                                 br_1=new BufferedReader(isr_1);
  521.                                                                 tmp=br_1.readLine();
  522.                                                                 if(!tmp.equals(""))
  523.                                                                 {
  524.                                                                         passwd=tmp;
  525.                                                                 }
  526.                                                         }
  527.                                                         catch(IOException e_1)
  528.                                                         {
  529.                                                                 System.out.println(e_1);
  530.                                                         }
  531.                                                 }
  532.                                         }
  533.                                         while(c1!='0');
  534.                                 }
  535.                                 else if(GUI==1)
  536.                                 {
  537.                                 }
  538.                                 //<<Set connection
  539.                         }
  540.                         else if(c1=='2')
  541.                         {
  542.                                 conffile=save_conf(conffile,LG,GUI);
  543.                         }
  544.                         else if(c1=='3')
  545.                         {
  546.                                 db_connect(LG,GUI,mode);
  547.                                 break;
  548.                         }
  549.                         else if(c1=='0')
  550.                         {
  551.                                 failed=1;
  552.                                 break;
  553.                         }
  554.                 }
  555.                 while(failed==1);
  556.         }
  557.         private static void con_test(char LG,int GUI)
  558.         {
  559.                 char c1,c2;
  560.                      c1=c2='\0';
  561.                 //>;>;Test driver
  562.                 try
  563.                 {
  564.                         examine(LG,GUI);
  565.                         Class.forName(driver);
  566.                         //>;>;Test connection
  567.                         try
  568.                         {
  569.                                 connect=DriverManager.getConnection(url,login,passwd);
  570.                                 if(GUI==0)
  571.                                 {
  572.                                         screen_up();
  573.                                         if(LG=='C')
  574.                                         {
  575.                                                 System.out.print("测试成功!\n\n");
  576.                                                 System.out.print("按“回车”键断开连接。\n\n");
  577.                                         }
  578.                                         else
  579.                                         {
  580.                                                 System.out.print("MADE IT !!\n\n");
  581.                                                 System.out.print("Press key<Enter>; to close connection.\n\n");
  582.                                         }
  583.                                         //>;>;Wait
  584.                                         do
  585.                                         {
  586.                                                 try
  587.                                                 {
  588.                                                         c2=(char)System.in.read();
  589.                                                 }
  590.                                                 catch(IOException e_1)
  591.                                                 {
  592.                                                         System.out.println(e_1);
  593.                                                 }
  594.                                         }
  595.                                         while(c2!='\n');
  596.                                         //<<Wait
  597.                                 }
  598.                                 else
  599.                                 {
  600.                                 }
  601.                                 connect.close();
  602.                                 failed=1;
  603.                         }
  604.                         catch(SQLException e_1)
  605.                         {
  606.                                 failed=1;
  607.                                 error_1(e_1,LG,GUI);
  608.                         }
  609.                         //<<Teat connection
  610.                 }
  611.                 catch(java.lang.ClassNotFoundException e_1)
  612.                 {
  613.                         failed=1;
  614.                         error_2(e_1,LG,GUI);
  615.                 }
  616.                 //<<Test driver
  617.         }
  618.         private static void db_connect(char LG,int GUI,int mode)
  619.         {
  620.                 char c1,c2;
  621.                      c1=c2='\0';
  622.                 try
  623.                 {
  624.                         if(mode==0||mode==1)
  625.                         {
  626.                                 examine(LG,GUI);
  627.                         }
  628.                         Class.forName(driver);
  629.                         try
  630.                         {
  631.                                 connect=DriverManager.getConnection(url,login,passwd);
  632.                                 failed=0;
  633.                         }
  634.                         catch(SQLException e_1)
  635.                         {
  636.                                 failed=1;
  637.                                 if(mode==0||mode==1)
  638.                                 {
  639.                                         error_1(e_1,LG,GUI);
  640.                                 }
  641.                         }
  642.                 }
  643.                 catch(java.lang.ClassNotFoundException e_1)
  644.                 {
  645.                         failed=1;
  646.                         if(mode==0||mode==1)
  647.                         {
  648.                                 error_2(e_1,LG,GUI);
  649.                         }
  650.                 }
  651.         }
  652.         private static void error_1(Exception e_1,char LG,int GUI)
  653.         {
  654.                 char c1,c2;
  655.                      c1=c2='\0';
  656.                 if(GUI==0)
  657.                 {
  658.                         screen_up();
  659.                         System.out.print(e_1+"\n\n");
  660.                         if(LG=='C')
  661.                         {
  662.                                 System.out.print("目标数据库连接失败!\n\n");
  663.                                 System.out.print("按“回车”键继续。\n\n");
  664.                         }
  665.                         else
  666.                         {
  667.                                 System.out.print("Failed to connect to obiect database!\n\n");
  668.                                 System.out.print("Press any key<Enter>; to continue.\n\n");
  669.                         }
  670.                         //>;>;Wait
  671.                         do
  672.                         {
  673.                                 try
  674.                                 {
  675.                                         c2=(char)System.in.read();
  676.                                 }
  677.                                 catch(IOException e_2)
  678.                                 {
  679.                                         System.out.println(e_2);
  680.                                 }
  681.                         }
  682.                         while(c2!='\n');
  683.                         //<<Wait
  684.                 }
  685.                 else
  686.                 {
  687.                 }
  688.         }
  689.         private static void error_2(Exception e_1,char LG,int GUI)
  690.         {
  691.                 char c1,c2;
  692.                      c1=c2='\0';
  693.                 if(GUI==0)
  694.                 {
  695.                         screen_up();
  696.                         System.out.print(e_1+"\n\n");
  697.                         if(LG=='C')
  698.                         {
  699.                                 System.out.print("驱动加载失败!\n\n");
  700.                                 System.out.print("当前驱动为:\n"+driver+"\n\n");
  701.                                 System.out.print("按“回车”键继续。\n\n");
  702.                         }
  703.                         else
  704.                         {
  705.                                 System.out.print("Driver failed!\n\n");
  706.                                 System.out.print("Current dirver is:\n"+driver+"\n\n");
  707.                                 System.out.print("Press any key<Enter>; to continue.\n\n");
  708.                         }
  709.                         //>;>;Wait
  710.                         do
  711.                         {
  712.                                 try
  713.                                 {
  714.                                         c2=(char)System.in.read();
  715.                                 }
  716.                                 catch(IOException e_2)
  717.                                 {
  718.                                 System.out.println(e_2);
  719.                                 }
  720.                         }
  721.                         while(c2!='\n');
  722.                         //<<Wait
  723.                 }
  724.                 else
  725.                 {
  726.                 }
  727.         }
  728.         private static void examine(char LG,int GUI)
  729.         {
  730.                 InputStreamReader isr_1;
  731.                 String input_1;
  732.                 BufferedReader br_1;
  733.                 if(GUI==0)
  734.                 {
  735.                         screen_up();
  736.                         if(LG=='C')
  737.                         {
  738.                                 if(driver.equals(""))
  739.                                 {
  740.                                         System.out.print("请输入jdbc驱动,然后按“回车”键:\n\n");
  741.                                         try
  742.                                         {
  743.                                                 isr_1=new InputStreamReader(System.in);
  744.                                                 br_1=new BufferedReader(isr_1);
  745.                                                 driver=br_1.readLine();
  746.                                         }
  747.                                         catch(IOException e_1)
  748.                                         {
  749.                                                 System.out.println(e_1);
  750.                                         }
  751.                                 }
  752.                                 if(url.equals(""))
  753.                                 {
  754.                                         System.out.print("请输入目标URL,然后按“回车”键:\n\n");
  755.                                         try
  756.                                         {
  757.                                                 isr_1=new InputStreamReader(System.in);
  758.                                                 br_1=new BufferedReader(isr_1);
  759.                                                 url=br_1.readLine();
  760.                                         }
  761.                                         catch(IOException e_1)
  762.                                         {
  763.                                                 System.out.println(e_1);
  764.                                         }
  765.                                 }
  766.                                 if(login.equals(""))
  767.                                 {
  768.                                         System.out.print("请输入登录帐号,然后按“回车”键:\n\n");
  769.                                         try
  770.                                         {
  771.                                                 isr_1=new InputStreamReader(System.in);
  772.                                                 br_1=new BufferedReader(isr_1);
  773.                                                 login=br_1.readLine();
  774.                                         }
  775.                                         catch(IOException e_1)
  776.                                         {
  777.                                                 System.out.println(e_1);
  778.                                         }
  779.                                 }
  780.                                 if(passwd.equals(""))
  781.                                 {
  782.                                         System.out.print("请输入登录口令,然后按“回车”键:\n\n");
  783.                                         try
  784.                                         {
  785.                                                 isr_1=new InputStreamReader(System.in);
  786.                                                 br_1=new BufferedReader(isr_1);
  787.                                                 passwd=br_1.readLine();
  788.                                         }
  789.                                         catch(IOException e_1)
  790.                                         {
  791.                                                 System.out.println(e_1);
  792.                                         }
  793.                                 }
  794.                         }
  795.                         else
  796.                         {
  797.                                 if(driver.equals(""))
  798.                                 {
  799.                                         System.out.print("Input the jdbc-driver, then press\n");
  800.                                         System.out.print("key<Enter>;:\n\n");
  801.                                         try
  802.                                         {
  803.                                                 isr_1=new InputStreamReader(System.in);
  804.                                                 br_1=new BufferedReader(isr_1);
  805.                                                 driver=br_1.readLine();
  806.                                         }
  807.                                         catch(IOException e_1)
  808.                                         {
  809.                                                 System.out.println(e_1);
  810.                                         }
  811.                                 }
  812.                                 if(url.equals(""))
  813.                                 {
  814.                                         System.out.print("Input the object URL, then press\n");
  815.                                         System.out.print("key<Enter>;:\n\n");
  816.                                         try
  817.                                         {
  818.                                                 isr_1=new InputStreamReader(System.in);
  819.                                                 br_1=new BufferedReader(isr_1);
  820.                                                 url=br_1.readLine();
  821.                                         }
  822.                                         catch(IOException e_1)
  823.                                         {
  824.                                                 System.out.println(e_1);
  825.                                         }
  826.                                 }
  827.                                 if(login.equals(""))
  828.                                 {
  829.                                         System.out.print("Input your login name, then press\n");
  830.                                         System.out.print("key<Enter>;:\n\n");
  831.                                         try
  832.                                         {
  833.                                                 isr_1=new InputStreamReader(System.in);
  834.                                                 br_1=new BufferedReader(isr_1);
  835.                                                 login=br_1.readLine();
  836.                                         }
  837.                                         catch(IOException e_1)
  838.                                         {
  839.                                                 System.out.println(e_1);
  840.                                         }
  841.                                 }
  842.                                 if(passwd.equals(""))
  843.                                 {
  844.                                         System.out.print("Input your login password, then press\n");
  845.                                         System.out.print("key<Enter>;:\n\n");
  846.                                         try
  847.                                         {
  848.                                                 isr_1=new InputStreamReader(System.in);
  849.                                                 br_1=new BufferedReader(isr_1);
  850.                                                 passwd=br_1.readLine();
  851.                                         }
  852.                                         catch(IOException e_1)
  853.                                         {
  854.                                                 System.out.println(e_1);
  855.                                         }
  856.                                 }
  857.                         }
  858.                 }
  859.                 else
  860.                 {
  861.                 }
  862.         }
  863. }
复制代码

读取配置文件里的字符串“Stringld.java”
  1. import java.io.*;
  2. public class Stringld
  3. {
  4.         FileInputStream inStream=null;
  5.         String content="";
  6.         public void stringld(String in) throws Exception
  7.         {
  8.                 Core(in);
  9.         }
  10.         private void Core(String filename) throws Exception
  11.         {
  12.                 int readbyte;
  13.                 inStream=new FileInputStream(filename);
  14. System.out.print("\n[FileReaderString End]\n");
  15.                 while((readbyte=inStream.read())!=-1)
  16.                 {
  17.                         content=content+String.valueOf((char)readbyte);
  18.                 }
  19.                 if(inStream!=null)
  20.                 {
  21.                         inStream.close();
  22.                 }
  23. System.out.print("\n[FileReaderString End]\n");
  24.         }
  25.         public String up_send()
  26.         {
  27.                 return content;
  28.         }
  29. }
复制代码

把字符串写入配置文件“Stringsv.java”
  1. import java.io.*;
  2. public class Stringsv
  3. {
  4.         FileOutputStream outStream=null;
  5.         public void stringsv(String in_1,String in_2) throws Exception
  6.         {
  7.                         Core(in_1,in_2);
  8.         }
  9.         private void Core(String filename,String content) throws Exception
  10.         {
  11. System.out.print("\n[FileWriterString Start]\n");
  12.                         outStream=new FileOutputStream(filename);
  13.                         outStream.write(content.getBytes());
  14.                         if(outStream!=null)
  15.                         {
  16.                                 outStream.close();
  17.                         }
  18. System.out.print("\n[FileWriterString End]\n");
  19.         }
  20. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2003-09-26 09:46 |只看该作者

[java]制作中的跨数据库连接器。

呵呵,就是对Jdbc来一个外壳吗?

支持原创!!

论坛徽章:
0
3 [报告]
发表于 2003-09-26 10:26 |只看该作者

[java]制作中的跨数据库连接器。

[quote]原帖由 "rollingpig"]呵呵,就是对Jdbc来一个外壳吗?[/quote 发表:


没说不是啊。好多东西用起来不方便,不是功能不够,只是缺乏象样的包装,那么我就来做包装咯 :wink:

论坛徽章:
0
4 [报告]
发表于 2003-09-30 00:17 |只看该作者

[java]制作中的跨数据库连接器。

主体部分终于完成了,可以添加图形界面了。

开发中,暂时没有说明文档,见谅。

下载后,把后缀名改为tgz,解压。

09292003A.jpg (12.03 KB, 下载次数: 86)

09292003A.jpg

论坛徽章:
0
5 [报告]
发表于 2003-09-30 08:29 |只看该作者

[java]制作中的跨数据库连接器。

感谢自由狼-台风0朋友分享技术成果。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP