java JDBC 提高程序可移植性 (转)

java JDBC 提高程序可移植性 (转)[@more@]

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

很多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+" "+driverStr+" "+userId+" "+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源文件放在同一个目录下面。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10790690/viewspace-961269/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10790690/viewspace-961269/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值