Chinaunix首页 | 论坛 | 博客
  • 博客访问: 316365
  • 博文数量: 101
  • 博客积分: 2324
  • 博客等级: 大尉
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 19:28
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(60)

2010年(38)

分类: 数据库开发技术

2011-03-21 14:27:06

1. 首先创建一个接口函数
  

import java.sql.Connection;

public interface IDBConnection {

    //获得数据库连接

    public Connection getConnection();
    //释放数据库连接

    public void closeConnection(Connection c);
    

}

2.与数据库建立连接

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBConnection implements IDBConnection {

    public void closeConnection(Connection c) {
        //判断 connection是否为null

        if(null != c ){
            try {
                if(!c.isClosed()){
                    //如果连接没有关闭

                    c.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
        }

    }

    public Connection getConnection() {
        //声明数据库连接

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(dbURL, userName, userPwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        
        return connection;
    }
    public DBConnection() {
        //读取jdbc.properties属性文件

        Properties prop = new Properties();
        //加载属性文件

    
        //加载驱动程序

        
        try {    
            
        
            prop.load(new FileInputStream("D:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/XXXX/src/jdbc.properties"));
            //读取属性

            userName = prop.getProperty("userName");
            dbURL = prop.getProperty("dbURL");
            userPwd = prop.getProperty("userPwd");
            driverName = prop.getProperty("driverName");
            Class.forName(driverName);
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        
    }
    private String userName;
    private String dbURL;
    private String userPwd;
    private String driverName;
}

3. 关于jdbc.properties
 name                  value
 userName               sa
  userPwd               XXXXX
 dbURL                  jdbc:sqlserver://localhost:1433:DatabaseName=XXX
 driverName             com.microsoft.sqlserver.jdvc.SQLServerDriver
4. 具体操作

mport java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.snmp4j.smi.OID;



import shark.zd.util.DBConnection;
import shark.zd.util.IDBConnection;

public class DBConcrol {
    
    //创建dbconnection

    static IDBConnection dbc = new DBConnection();
    
    static //获得connection

    Connection conn = dbc.getConnection();
    
    
    public static String getIndex(String Node_Name) throws SQLException
    {
        String ReturnValue = null;
        Statement stmtStatement = conn.createStatement();
        String sql = "select * from NodeIndex where NodeName = '"+Node_Name+"'";
        stmtStatement.executeQuery(sql);
        ResultSet rs = stmtStatement.executeQuery(sql);
        while(rs.next())
        {
            ReturnValue = rs.getString("IndexVal");
            
        }
        
        
        return ReturnValue;
        
        
    }
    public static String saveTodb(String Table,String col,String value) throws SQLException
    {
        Statement stmtStatement1 = conn.createStatement();
        String sql = "select * from "+Table+" where "+col+"='"+value+"'";
        stmtStatement1.executeQuery(sql);
    
        java.sql.ResultSet rs = stmtStatement1.executeQuery(sql);
        java.sql.ResultSetMetaData metaData = rs.getMetaData();
        
        int count =0;
        
        //打印换行符

        System.out.println();
        //打印查询记录

        while(rs.next()){
            for(int i =1;i<metaData.getColumnCount();i++){                
            }
            count++;            
        }
    
        
        //if((stmtStatement.getResultSet()==null)&&(stmtStatement.getUpdateCount()==-1))

        if(count<=0)
        //if((stmtStatement.getMoreResults()==false)&&(stmtStatement.getUpdateCount()==-1))

        {
             sql = "insert into "+Table+" ("+col+") values ('"+value+"')";
             stmtStatement1.executeUpdate(sql);
            
        }
        else
        {
            
        }
        //stmtStatement.execute("delete from NSG9000_Chassis");

        
        return null;
        
    }

}


阅读(1522) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册