|
做大作业时想到的,设计方案中能够访问绝大多数关系数据库。
暂时保存。不少地方还有错误,也还没加图形界面。
但是在设计方案中,做好后它能同时适用于Console、X-Window和MS-Window$。
源文件如下:
带主方法的“test_platform.java”
[code:1]
import java.io.*;
public class test_platform
{
public static void main(String args[])
{
String string_1="[main(String args[]) start]";
String string_2="[main(String args[]) ended]";
System.out.println(string_1);
try
{
Test_1 A=new Test_1();//调用其他类中的非构造方法,要先创建这个类的对象
A.test_1();//通过该类调用此方法
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(string_2);
}
}
[/code:1]
调用连接器“Test_1.java”
[code:1]
//0.5.8-80
import java.io.*;
import java.sql.*;
public class Test_1
{
public void test_1()
{
String conffile="/usr2/home/root/work/java/tmp/db.conf";
int mode=0;
char LG='E';
int GUI=0;
int failed;
System.out.println("[test_1() start]");
Connection connect=null;
Statement C=null;
ResultSet D=null;
//String sql_1="create table tbtest_3(employee_number int,surname varchar(40));";
String sql_1="create table tbtest_5(opt_int int,opt_varchar40 varchar(40),opt_tinyint128 tinyint);";
DB_init B=new DB_init();
failed=B.db_init(conffile,mode,LG,GUI);
connect=B.db_result();
System.out.println("\t"+failed);
/*
try
{
C=connect.createStatement();
C.execute(sql_1);
//D=C.executeQuery();
}
catch(SQLException e)
{
System.out.println("[[["+e+"]]]");
}
*/
System.out.println("[test_1() ended]");
}
}
[/code:1]
jdbc连接器“DB_init.java”
[code:1]
//0.5.8-80
//LG:language
//org.gjt.mm.mysql.Driver
//jdbc:mysql://192.168.1.7/dbtest_1
//String sql_1="create table tbtest_x(opt_int int,opt_varchar40 varchar(40),opt_tinyint128 tinyint);";
import java.io.*;
import java.sql.*;
public class DB_init
{
public static Connection connect=null;
public static int failed=0;
private static String DBMS_name="MySQL";
private static String driver="org.gjt.mm.mysql.Driver";
private static String url="jdbc:mysql://192.168.1.1/dbtest_1";
private static String login="";
private static String passwd="";
public static int db_init(String conffile,int mode,char LG,int GUI)
{
System.out.println("[db_init(char LG,int GUI) start]");
System.out.println("\t"+conffile+"\t"+mode+"\t"+LG+"\t"+GUI);
failed=1;
load_conf(conffile,mode);
if(mode==0)
{
db_manual(conffile,LG,GUI);
}
else if(mode==1||mode==2)
{
db_connect(LG,GUI,mode);
}
System.out.println("[db_init(int mode,char LG,int GUI) ended]");
System.out.println(DBMS_name);
System.out.println(driver);
System.out.println(url);
System.out.println(login);
System.out.println(passwd);
System.out.println("\t"+conffile+"\t"+mode+"\t"+LG+"\t"+GUI);
System.out.println("[db_init(char LG,int GUI) ended]");
return failed;
}
public static Connection db_result()
{
return connect;
}
public static void screen_up()
{
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");
}
private static void load_conf(String conffile,int mode)
{
String dbconf="";
char tmp_0[]=new char[0];
char tmp_1[]=new char[0];
char tmp_2[]=new char[0];
char tmp_3[]=new char[0];
char tmp_4[]=new char[0];
char tmp_5[]=new char[0];
if(conffile!="")
{
try
{
Stringld loader=new Stringld();
loader.stringld(conffile);
dbconf=loader.up_send();
}
catch(Exception e_1)
{
dbconf="";
if(mode==1||mode==2)
{
System.out.println(e_1);
}
}
}
tmp_0=dbconf.toCharArray();
System.out.print("dbconf:\n"+dbconf+"\n");
System.out.print("dbconf:\n"+tmp_0+"\n");
}
private static String save_conf(String conffile,char LG,int GUI)
{
String dbconf;
InputStreamReader isr_1;
BufferedReader br_1;
String tmp="";
char c1='\0';
if(GUI==0)
{
screen_up();
System.out.print(conffile+"\n\n");
if(conffile=="")
{
if(LG=='C')
{
System.out.print("当前连接将被保存进一个文件。为这个文件命\n");
System.out.print("名,请先输入文件名,然后按“回车”键:\n\n");
}
else
{
System.out.print("Current connection will be save into a\n");
System.out.print("file. To name this file, firstly input\n");
System.out.print("the name, then press key<Enter>:\n\n");
}
}
else
{
if(LG=='C')
{
System.out.print("直接按“回车”键,当前连接将被保存到这个\n");
System.out.print("文件,它里面原有的记录将被冲掉;\n\n");
System.out.print("如果不想冲掉,请输入一个新文件名,然后按\n");
System.out.print("“回车”键:\n\n");
}
else
{
System.out.print("Directly press key<Enter> to save current\n");
System.out.print("connection into this file and broom the\n");
System.out.print("old note;\n\n");
System.out.print("Else you can input a new name and press\n");
System.out.print("key<Enter>.\n\n");
}
}
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
tmp=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
if(!tmp.equals(""))
{
conffile=tmp;
}
}
else
{
//use JFileChooser
}
dbconf="[DBMS_name]"+DBMS_name+"\n[driver]"+driver+"\n[url]"+url+"\n[login]"+login+"\n[passwd]"+passwd+"\n";
try
{
Stringsv saver=new Stringsv();
saver.stringsv(conffile,dbconf);
}
catch(Exception e_1)
{
if(GUI==0)
{
screen_up();
System.out.println(e_1);
do
{
try
{
c1=(char)System.in.read();
//System.out.println("c1="+c1);
}
catch(IOException e_2)
{
System.out.println(e_2);
}
}
while(c1!='\n');
}
else
{
}
}
System.out.println("dbconf:\n"+dbconf+"\n");
return conffile;
}
private static void db_manual(String conffile,char LG,int GUI)
{
//>>For StringInput
InputStreamReader isr_1;
String input_1;
BufferedReader br_1;
/*
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
input_1=br_1.readLine();
System.out.println("input_1:\n\t"+input_1);
}
catch(IOException e_1)
{
System.out.println(e_1);
}
*/
//<<For StringInput
//>>Model for one charater input
/*
try
{
c1=(char)System.in.read();
System.out.println("c1="+c1);
}
catch(IOException e_1)
{
System.out.println(e_1);
}
*/
//<<Model for one charater input
int mode=0;
//>>For temp
String tmp;
char c1,c2,c3;
int i1,i2,i3,i4;
//<<For temp
do
{
failed=1;
c1=c2='\0';
if(GUI==0)
{
screen_up();
//>>Selections
if(LG=='C')
{
System.out.print("以下项目中,你打算开始哪一个?请先输入它\n");
System.out.print("的序号,然后按“回车”键。\n\n");
System.out.print(" [Z: 关 于 ]\n\n");
System.out.print(" [1: 查 看 当 前 连 接 ]\n\n");
System.out.print(" [2: 保 存 当 前 连 接 ]\n\n");
System.out.print(" [3: 使 用 当 前 连 接 ]\n\n");
System.out.print(" [0: 放 弃 这 次 连 接 ]\n\n");
}
else
{
System.out.print("Which would you like to choose? Type its\n");
System.out.print("number and then press key<Enyer>.\n\n");
System.out.print(" [Z: ABOUT]\n\n");
System.out.print(" [1: View current connection]\n\n");
System.out.print(" [2: Save current connection]\n\n");
System.out.print(" [3: Adapt this connection]\n\n");
System.out.print(" [0: Abandon this connection]\n\n");
}
//<<Selections
//>>Choose
try
{
c1=(char)System.in.read();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
//<<Choose
//>>Eat for key<Enter>
c2='\0';
while(c1!='\n'&&c2!='\n')
{
try
{
c2=(char)System.in.read();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
//<<Eat for key<Enter>
}
else if(GUI==1)
{
}
//System.out.println("c1="+c1);
if(c1=='Z'||c1=='z')
{
if(GUI==0)
{
screen_up();
if(LG=='C')
{
System.out.print("《TFW-数据库连接器(Java)》\n");
System.out.print("版本:0.5.8-80\n");
System.out.print("========================================\n");
System.out.print("模型:\n");
System.out.print("\t自由狼-台风\n");
System.out.print("\t杨文亮(教员)\n");
System.out.print("代码:\n");
System.out.print("\t自由狼-台风\n");
System.out.print("\t祁可闻\n");
System.out.print("测试:\n");
System.out.print("\t自由狼-台风\n");
System.out.print("\t祁可闻\n");
System.out.print("版权释放:\n");
System.out.print("\t自由狼-台风\n");
System.out.print("========================================\n");
System.out.print("按“回车”键返回。");
}
else
{
System.out.print("\"TFW-DATABASE-CONNECTOR(Java)\"\n");
System.out.print("Version: 0.5.8-80\n");
System.out.print("========================================\n");
System.out.print("Model by:\n");
System.out.print("\tTyphoon.Free.Wolf\n");
System.out.print("\tYangWenliang(Teacher)\n");
System.out.print("Code by:\n");
System.out.print("\tTyphoon.Free.Wolf\n");
System.out.print("\tQiKewen\n");
System.out.print("Test by:\n");
System.out.print("\tTyphoon.Free.Wolf\n");
System.out.print("\tQiKewen\n");
System.out.print("Copyleft by:\n");
System.out.print("\tTyphoon.Free.Wolf\n");
System.out.print("========================================\n");
System.out.print("Press key<Enter> to go back.");
}
//>>Wait
do
{
try
{
c1=(char)System.in.read();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
while(c1!='\n');
//<<Wait
}
else if(GUI==1)
{
}
}
else if(c1=='1')
{
//>>Set connection
if(GUI==0)
{
do
{
screen_up();
//>>Selections
if(LG=='C')
{
System.out.print("以下项目中,你打算开始哪一个?请先输入它\n");
System.out.print("的序号,然后按“回车”键。\n\n");
System.out.print("[1:测试当前连接]\n");
System.out.print("[2:设定这个]当前数据库管理系统:"+DBMS_name+"\n");
System.out.print("[3:设定这个]当前的jdbc驱动:"+driver+"\n");
System.out.print("[4:设定这个]当前数据库的URL:"+url+"\n");
System.out.print("[5:设定这个]当前数据库登录用户:"+login+"\n");
System.out.print("[6:设定这个]当前数据库用户口令:"+passwd+"\n");
System.out.print("[0: 什么都不做 ]\n");
System.out.print("\n");
}
else
{
System.out.print("Which would you like to choose? Type its\n");
System.out.print("number and then press key<Enyer>.\n\n");
System.out.print("[1:Test current connection]\n");
System.out.print("[2:Set it]Current DatabaseManageSystem:"+DBMS_name+"\n");
System.out.print("[3:Set it]Current \"jdbc-driver\":"+driver+"\n");
System.out.print("[4:Set it]Current URL of the database:"+url+"\n");
System.out.print("[5:Set it]Current \"login\" name of the\ndatabase:"+login+"\n");
System.out.print("[6:Set it]Current \"login\" password of\nthe database:"+passwd+"\n");
System.out.print("[0: Do nothing ]\n");
System.out.print("\n");
}
//<<Selections
//>>Choose
try
{
c1=(char)System.in.read();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
//<<Choose
//>>Eat for key<Enter>
c2='\0';
while(c1!='\n'&&c2!='\n')
{
try
{
c2=(char)System.in.read();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
//<<Eat for key<Enter>
if(c1=='1')
{
con_test(LG,GUI);
}
else if(c1=='2')
{
screen_up();
System.out.print(DBMS_name+"\n\n");
if(LG=='C')
{
System.out.print("输入,然后按“回车”键:\n\n\n");
}
else
{
System.out.print("Input, the press key<Enter>:\n\n\n");
}
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
tmp=br_1.readLine();
if(!tmp.equals(""))
{
DBMS_name=tmp;
}
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
else if(c1=='3')
{
screen_up();
System.out.print(driver+"\n\n");
if(LG=='C')
{
System.out.print("输入,然后按“回车”键:\n\n\n");
}
else
{
System.out.print("Input, the press key<Enter>:\n\n\n");
}
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
tmp=br_1.readLine();
if(!tmp.equals(""))
{
driver=tmp;
}
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
else if(c1=='4')
{
screen_up();
System.out.print(url+"\n\n");
if(LG=='C')
{
System.out.print("输入,然后按“回车”键:\n\n\n");
}
else
{
System.out.print("Input, the press key<Enter>:\n\n\n");
}
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
tmp=br_1.readLine();
if(!tmp.equals(""))
{
url=tmp;
}
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
else if(c1=='5')
{
screen_up();
System.out.print(login+"\n\n");
if(LG=='C')
{
System.out.print("输入,然后按“回车”键:\n\n\n");
}
else
{
System.out.print("Input, the press key<Enter>:\n\n\n");
}
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
tmp=br_1.readLine();
if(!tmp.equals(""))
{
login=tmp;
}
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
else if(c1=='6')
{
screen_up();
System.out.print(passwd+"\n\n");
if(LG=='C')
{
System.out.print("输入,然后按“回车”键:\n\n\n");
}
else
{
System.out.print("Input, the press key<Enter>:\n\n\n");
}
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
tmp=br_1.readLine();
if(!tmp.equals(""))
{
passwd=tmp;
}
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
}
while(c1!='0');
}
else if(GUI==1)
{
}
//<<Set connection
}
else if(c1=='2')
{
conffile=save_conf(conffile,LG,GUI);
}
else if(c1=='3')
{
db_connect(LG,GUI,mode);
break;
}
else if(c1=='0')
{
failed=1;
break;
}
}
while(failed==1);
}
private static void con_test(char LG,int GUI)
{
char c1,c2;
c1=c2='\0';
//>>Test driver
try
{
examine(LG,GUI);
Class.forName(driver);
//>>Test connection
try
{
connect=DriverManager.getConnection(url,login,passwd);
if(GUI==0)
{
screen_up();
if(LG=='C')
{
System.out.print("测试成功!\n\n");
System.out.print("按“回车”键断开连接。\n\n");
}
else
{
System.out.print("MADE IT !!\n\n");
System.out.print("Press key<Enter> to close connection.\n\n");
}
//>>Wait
do
{
try
{
c2=(char)System.in.read();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
while(c2!='\n');
//<<Wait
}
else
{
}
connect.close();
failed=1;
}
catch(SQLException e_1)
{
failed=1;
error_1(e_1,LG,GUI);
}
//<<Teat connection
}
catch(java.lang.ClassNotFoundException e_1)
{
failed=1;
error_2(e_1,LG,GUI);
}
//<<Test driver
}
private static void db_connect(char LG,int GUI,int mode)
{
char c1,c2;
c1=c2='\0';
try
{
if(mode==0||mode==1)
{
examine(LG,GUI);
}
Class.forName(driver);
try
{
connect=DriverManager.getConnection(url,login,passwd);
failed=0;
}
catch(SQLException e_1)
{
failed=1;
if(mode==0||mode==1)
{
error_1(e_1,LG,GUI);
}
}
}
catch(java.lang.ClassNotFoundException e_1)
{
failed=1;
if(mode==0||mode==1)
{
error_2(e_1,LG,GUI);
}
}
}
private static void error_1(Exception e_1,char LG,int GUI)
{
char c1,c2;
c1=c2='\0';
if(GUI==0)
{
screen_up();
System.out.print(e_1+"\n\n");
if(LG=='C')
{
System.out.print("目标数据库连接失败!\n\n");
System.out.print("按“回车”键继续。\n\n");
}
else
{
System.out.print("Failed to connect to obiect database!\n\n");
System.out.print("Press any key<Enter> to continue.\n\n");
}
//>>Wait
do
{
try
{
c2=(char)System.in.read();
}
catch(IOException e_2)
{
System.out.println(e_2);
}
}
while(c2!='\n');
//<<Wait
}
else
{
}
}
private static void error_2(Exception e_1,char LG,int GUI)
{
char c1,c2;
c1=c2='\0';
if(GUI==0)
{
screen_up();
System.out.print(e_1+"\n\n");
if(LG=='C')
{
System.out.print("驱动加载失败!\n\n");
System.out.print("当前驱动为:\n"+driver+"\n\n");
System.out.print("按“回车”键继续。\n\n");
}
else
{
System.out.print("Driver failed!\n\n");
System.out.print("Current dirver is:\n"+driver+"\n\n");
System.out.print("Press any key<Enter> to continue.\n\n");
}
//>>Wait
do
{
try
{
c2=(char)System.in.read();
}
catch(IOException e_2)
{
System.out.println(e_2);
}
}
while(c2!='\n');
//<<Wait
}
else
{
}
}
private static void examine(char LG,int GUI)
{
InputStreamReader isr_1;
String input_1;
BufferedReader br_1;
if(GUI==0)
{
screen_up();
if(LG=='C')
{
if(driver.equals(""))
{
System.out.print("请输入jdbc驱动,然后按“回车”键:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
driver=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
if(url.equals(""))
{
System.out.print("请输入目标URL,然后按“回车”键:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
url=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
if(login.equals(""))
{
System.out.print("请输入登录帐号,然后按“回车”键:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
login=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
if(passwd.equals(""))
{
System.out.print("请输入登录口令,然后按“回车”键:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
passwd=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
}
else
{
if(driver.equals(""))
{
System.out.print("Input the jdbc-driver, then press\n");
System.out.print("key<Enter>:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
driver=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
if(url.equals(""))
{
System.out.print("Input the object URL, then press\n");
System.out.print("key<Enter>:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
url=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
if(login.equals(""))
{
System.out.print("Input your login name, then press\n");
System.out.print("key<Enter>:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
login=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
if(passwd.equals(""))
{
System.out.print("Input your login password, then press\n");
System.out.print("key<Enter>:\n\n");
try
{
isr_1=new InputStreamReader(System.in);
br_1=new BufferedReader(isr_1);
passwd=br_1.readLine();
}
catch(IOException e_1)
{
System.out.println(e_1);
}
}
}
}
else
{
}
}
}
[/code:1]
读取配置文件里的字符串“Stringld.java”
[code:1]
import java.io.*;
public class Stringld
{
FileInputStream inStream=null;
String content="";
public void stringld(String in) throws Exception
{
Core(in);
}
private void Core(String filename) throws Exception
{
int readbyte;
inStream=new FileInputStream(filename);
System.out.print("\n[FileReaderString End]\n");
while((readbyte=inStream.read())!=-1)
{
content=content+String.valueOf((char)readbyte);
}
if(inStream!=null)
{
inStream.close();
}
System.out.print("\n[FileReaderString End]\n");
}
public String up_send()
{
return content;
}
}
[/code:1]
把字符串写入配置文件“Stringsv.java”
[code:1]
import java.io.*;
public class Stringsv
{
FileOutputStream outStream=null;
public void stringsv(String in_1,String in_2) throws Exception
{
Core(in_1,in_2);
}
private void Core(String filename,String content) throws Exception
{
System.out.print("\n[FileWriterString Start]\n");
outStream=new FileOutputStream(filename);
outStream.write(content.getBytes());
if(outStream!=null)
{
outStream.close();
}
System.out.print("\n[FileWriterString End]\n");
}
}
[/code:1] |
|