<%@ page contentType="text/html; charset=gb2312"%> java JDBC 提高程序可移植性
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
java JDBC 提高程序可移植性
     发布者: 发布时间:2007-01-03
很多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技术网(www.java114.com))
 
更多精彩文章:
递归函数之JAVA演绎
我编的JAVA程序,欢迎高手提宝贵意见和建议.
通过java提供的URL类包读取网上的文件
框架(javascript)
java网络五子棋的源代码
Java Reflection (JAVA反射)
 
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号