package Longin;
import java.net.URL;
import java.util.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.Properties;
/**
 * @模块: Dataopen.java
 * @描述: 数据库连接类
 * @技术: Swing/AWT
 * @版权: 中国科学院西安网络中心软件部
 * @版本: Client V1.0
 * @作者: yjt
 * @时间: 2007/05/20
 **/
public class Dataopen //方法1
{
  public  static java.sql.Connection  con = null;
//  private final String url = "jdbc:microsoft:sqlserver://";   //直接赋值法
//  private final String serverName= "localhost";
//  private final String portNumber = "1433";
//  private final String databaseName= "student";
//  private final String userName = "sa";
//  private final String password = "";
//  private final String selectMethod = "cursor";
   public static String name = "";    //用户名        //方法2
   public static int  BS=0;    //标识
   public static int  BS1=0;    //标识
   public static int  BS2=0;    //标识控制显示那各界面
   private Properties props = new Properties(); //定义一个Properties 用来存放dbhost dbuser dbpassword的值
   private String currentName; // 当前的属性名
   private StringBuffer currentValue = new StringBuffer(); // 属性值缓存
   private String url;
   private String serverName;
   private String portNumber;
   private String databaseName;
   private String selectMethod;
   private String userName;
   private String password;
  public Dataopen()
  {
     // getConnString();
      getConnectionUrl();
  }
  public String getConnectionUrl(){
//      ResourceBundle resource = ResourceBundle.getBundle("mypackage1.conmysql");  //从文件中获得参数。
//      url=resource.getString("url");
//      serverName=resource.getString("serverName");
//      portNumber=resource.getString("portNumber");
//      databaseName=resource.getString("databaseName");
//      selectMethod=resource.getString("selectMethod");
//      userName=resource.getString("userName");
//      password=resource.getString("password");
       //this.getConnString();
       return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
  }
  public java.sql.Connection getConnection(){
    try{
      // A. 注册 SQL Server JDBC 驱动程序
       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
       //B. 创建新数据库连接
      con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
        
      
        if(con!=null) {}//System.out.println("SQL Server Connection Successful!");
    }catch(Exception e){
      e.printStackTrace();
      //System.out.println("Error SQL Server Trace in getConnection() : " + e.getMessage());
    }
    return con;
  }
  //关闭数据库
  public void closeSQLServerConnection(){
    try{
      if(con!=null)
        con.close();
        con=null;
      }catch(Exception e){
        e.printStackTrace();
      }
  }
public static void main(String[] args)  //方法3
  {
  }
    public void getConnString(){
        //从XML文件中获取参数的方法
        try{
            //获取SAX工厂对象
            SAXParserFactory factory = SAXParserFactory.newInstance();       
            //获取SAX解析
            SAXParser saxParser = factory.newSAXParser();
            factory.setNamespaceAware(false);
            factory.setValidating(false);
            
            // 解析XML
            DefaultHandler handler = new DefaultHandler() {
                // 第一步,定义开始解析元素的方法。这里是将<xxx>中的名称xxx提取出来。
                public void startElement(String uri, String localName, String qName, Attributes attributes) 
                throws SAXException {
                    currentName = qName;
                }                // 第二步,这里是将<xxx></xxx>之间的值加入到currentValue
                public void characters(char[] ch, int start, int length) throws SAXException { 
                    currentValue.delete(0, currentValue.length()); // 清空                    
                    currentValue.append(ch, start, length);
                }                // 第三步,在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中
                public void endElement(String uri, String localName, String qName) throws SAXException 
                {
                    props.put(qName.toLowerCase(), currentValue.toString().trim());
                }                    
            };            //得到配置文件myConfig.xml所在目录。
            URL confURL = this.getClass().getClassLoader().getResource("mypackage1/conmysql.xml");
            //将解析器和解析对象myConfig.xml联系起来,开始解析 
            saxParser.parse(confURL.toString(), handler);             //获取解析成功后的属性以后我们其他应用程序只要调用本程序的props就可以提取出属性名称和值了
//            this.url = props.getProperty("url");   //XML文件中的定义标记要为小写字母。
//            this.serverName = props.getProperty("dbhost");
//            this.portNumber = props.getProperty("portnumber");
//            this.databaseName= props.getProperty("dbname");
//            this.selectMethod=props.getProperty("method");
//            this.userName = props.getProperty("dbuser");
//            this.password = props.getProperty("password");
//       
        } catch (SAXException sax) {
            sax.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
    }  }
以上3种方法哪种方法好一点,在就是第三种方法看不懂,请教高手!!(这是学生管理系统里面的一段连接数据库的代码)

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【xiaomuu99441166】截止到2008-06-28 01:26:59的历史汇总数据(不包括此帖):
    发帖数:2                  发帖分:40                 
    结贴数:2                  结贴分:40                 
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    喜欢哪种就用那种
    方法3是把连接的相关信息写到XML中 然后通过解析来获得这些信息