|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
很多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源文件放在同一个目录下面。 返回类别: 教程 上一教程: java的“别名”以及clone机制 下一教程: 有关jive论坛搜索中文化的支持详解! 您可以阅读与"java JDBC 提高程序可移植性"相关的教程: · 如何优化JAVA程序设计和编码,提高JAVA性能 · 提高Java程序内存效率 · 漫谈Java程序设计中的接口应用 · 一个经典的JAVA APPLET时钟程序(一) · 用JAVA设计浮动按钮程序 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |