陈小洪   南昌航空工业学院计算机系
  很多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源文件放在同一个目录下面。  
 
  |