zoukankan      html  css  js  c++  java
  • Java-编写一个jdbc操作类

    1.通过读取文件配置

    package 数据库操作类;  
      
      
        /* * Db.java 
        Created on 2007年8月20日, 上午 8:37 
        */  
        import java.io.*;  
        import java.sql.*;  
        import java.util.Properties;  
        public class DB {  
            private String driver;  
            private String url;  
            private String user;  
            private String password;  
            private Connection conn;  
            private Statement stm;  
            private ResultSet rs;  
            public DB(){  
                this("DBConf.properties");  
            }  
            public DB(String conf) {  
                loadProperties(conf);  
                setConn();  
            }  
            public Connection getConn(){  
                return this.conn;  
            }  
          //handle the properties file to get the informations for connection  
            private void loadProperties(String conf){  
                Properties props = new Properties();  
                try {  
                    props.load(new FileInputStream(conf));  
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                this.driver = props.getProperty("driver");  
                this.url = props.getProperty("url");  
                this.user = props.getProperty("user");  
                this.password = props.getProperty("password");  
            }  
            //implement the Connection  
            private void setConn(){  
                try {  
                    Class.forName(driver);  
                    this.conn = DriverManager.getConnection(url,user,password);  
                } catch(ClassNotFoundException classnotfoundexception) {  
                      classnotfoundexception.printStackTrace();  
                    System.err.println("db: " + classnotfoundexception.getMessage());  
                } catch(SQLException sqlexception) {  
                    System.err.println("db.getconn(): " + sqlexception.getMessage());  
                }  
            }  
               public void doInsert(String sql) {  
                try {  
                    Statement statement = conn.createStatement();  
                    int i = stm.executeUpdate(sql);  
                } catch(SQLException sqlexception) {  
                    System.err.println("db.executeInset:" + sqlexception.getMessage());  
                }  
            }  
            public void doDelete(String sql) {  
                try {  
                    stm = conn.createStatement();  
                    int i = stm.executeUpdate(sql);  
                } catch(SQLException sqlexception) {  
                    System.err.println("db.executeDelete:" + sqlexception.getMessage());  
                }  
            }  
            public void doUpdate(String sql) {  
                try {  
                    stm = conn.createStatement();  
                    int i = stm.executeUpdate(sql);  
                } catch(SQLException sqlexception) {  
                    System.err.println("db.executeUpdate:" + sqlexception.getMessage());  
                }  
            }  
              
            public ResultSet doSelect(String sql) {  
                try {  
                    stm = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);  
                    rs = stm.executeQuery(sql);  
                } catch(SQLException sqlexception) {  
                    System.err.println("db.executeQuery: " + sqlexception.getMessage());  
                }  
                return rs;  
            }  
            public static void main(String[] args){  
                try{  
                    DB db = new DB();  
                    Connection conn = db.getConn();  
                    if(conn != null && !conn.isClosed()) {  
                        System.out.println("連結成功");  
                        ResultSet rs = db.doSelect("select * from content");  
                        while(rs.next()){  
                            System.out.println(rs.getString(1)+":"+rs.getString(2)+":"+rs.getString(3));  
                          }  
                        rs.close();  
                        conn.close();  
                    }  
                }catch(SQLException e) {  
                    e.printStackTrace();  
                }  
            }    
        }  
  • 相关阅读:
    Oracle数据库部分迁至闪存存储方案
    RAC环境下误操作将数据文件添加到本地存储
    Oracle的窗口和自动任务
    ####### Scripts Summary #######
    plsql 操纵表数据的2种方式
    css 如何使图片与文字在div中居中展示?
    eclipse svn新增文件不显示在文件列表,只有修改文件可以提交!
    js 正则表达式校验必须包含字母、数字、特殊字符
    css 禁止录入中文
    POJ 1740:A New Stone Game
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4510650.html
Copyright © 2011-2022 走看看