java JDBC 提高程序可移植性

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

陈小洪 南昌航空工业学院计算机系

很多java初学者在开始接触JDBC编程的时候,在网上和大部分的教材上都是这样

介绍JDBC一般的程序:

//可以运行的完整程序

import java.sql.*;

public class DatabaseDemo

{

public static void main(String args[])

{

Connection con;

Statement stmt;

ResultSet rs;

//load the driver class

try

{//直接在程序里面写字符串 com.microsoft.jdbc.sqlserver.SQLServerDriver

//降低了程序的可移植性。

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

}catch(ClassNotFoundException e)

{

System.out.println(e.getMessage());

}

//get database connection ,statement and the ResultSet

try

{

con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs", "sa", "");

stmt=con.createStatement();

rs=stmt.executeQuery("select * from authors");

while(rs.next())

{

for(int i=1;i<=rs.getMetaData().getColumnCount();i++)

{

System.out.print(rs.getString(i)+" | ");

}

System.out.println();

}

}catch(SQLException e)

{

System.out.println(e.getMessage());

}

}

}

这个程序明显有一个问题,就是程序的可移植性很差,加入我现在不用sql server了,我要使用sybase或orcale

而程序已经大包了,怎么办。在这里我们可以像在vc ado中的 uld文件一样。使用我们的properties文件。把属性和对应的值写入属性文件。例如我们在属性文件 basic.properties 输入一些内容:

connectionURL:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs

driverManager:com.microsoft.jdbc.sqlserver.SQLServerDriver

userID:sa

password:

//注意属性与值之间要有一个冒号,英文模式,不是中文冒号。

我们可以通过 java.util包里面的Properties类来读取文件属性的值。

java.util.Properties pro=new java.util.Properties pro();

try

{

pro.load(new FileInputStream("basic.properties"));

}catch(IOException e){.......}

conSql=pro.getProperty("connectionURL");

driverStr=pro.getProperty("driverManager");

userId=pro.getProperty("userID");

password=pro.getProperty("password");

这样我们就可以得到properties中的属性值。当我们的应用程序要改用其他的数据库管理系统的时候我们只要

修改 properties文件就可以了。

把上面的程序完善一些(可以运行的)

/*

AA.java

*/

import java.util.*;

import java.io.*;

import java.sql.*;

public class AA

{

public static void main(String args[])

{

String conSql;

String driverStr;

String userId;

String password;

Connection con;

Statement stmt;

ResultSet rs;

Properties pro=new Properties();;

//load the properties file and get the proterties

try

{

pro.load(new FileInputStream("basic.properties"));

}catch(IOException e)

{

System.out.println(e.getMessage());

}

conSql=pro.getProperty("connectionURL");

driverStr=pro.getProperty("driverManager");

userId=pro.getProperty("userID");

password=pro.getProperty("password");

System.out.println(conSql+"\n"+driverStr+"\n"+userId+"\n"+password);

// load the driver class

try

{

Class.forName(driverStr);

}catch(ClassNotFoundException e)

{

System.out.println(e.getMessage());

}

// get the database connection

try

{

con=DriverManager.getConnection(conSql,userId,password);

String queryStr="select * from authors";

stmt=con.createStatement();

rs=stmt.executeQuery(queryStr);

while(rs.next())

{

for(int i=1;i<=rs.getMetaData().getColumnCount();i++)

{

System.out.print(rs.getString(i)+" | ");

}

System.out.println();

}

}catch(SQLException e)

{

System.out.println(e.getMessage());

}

}

}

注意了::::属性文件要和你的java源文件放在同一个目录下面。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航