⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db1.java

📁 java netbeans 学习程序合集
💻 JAVA
字号:
/*
 * DB1.java
 *
 * Created on 2007年12月20日, 下午8:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.Adam;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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;
/**
 *
 * @author Administrator
 */
public class DB1 {
     public static java.sql.Connection con = null;
    
    public static String name = ""; //用户名
    public static int BS = 0; //标识
    public static int BS1 = 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;
        private String connString;
    Statement stat=null;
    ResultSet rs = null;
    /** Creates a new instance of DB1 */
    public DB1() {
        getConnString();
    }
     public ResultSet SelectpassDb(String sql)
   {
       //String sql = "select password from userinfo where uid='"+uid+"'";
       try {
           stat = con.createStatement();
           rs=stat.executeQuery(sql);


       } catch (SQLException ex) {
           System.out.println(ex.getMessage());
       }
       
       return rs;


   }
      public void closeAccessConnection() {//定义关闭连接的方法
        try {
            if (con != null)
                con.close();
            con = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
      public String getConnectionUrl() {
       
        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 static void main(String[] args) {
        new DB1().getConnection();
    }
    public void closeSQLServerConnection() {
        try {
            if (con != null)
                con.close();
            con = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    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("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();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -