DButil

作者: AAnna珠 | 来源:发表于2019-04-11 17:02 被阅读0次
    
    package www.model.util;
    
    import java.io.FileInputStream;
    
    import java.io.FileNotFoundException;
    
    import java.io.IOException;
    
    import java.io.InputStream;
    
    import java.sql.Connection;
    
    import java.sql.DriverManager;
    
    import java.sql.SQLException;
    
    import java.util.Properties;
    
    public class DButil {
    
    private static String driver = null;
    
    private static String url = null;
    
    private static String username = null;
    
    private static String password = null;
    
    static{
    
    try {
    
    Properties pop = new Properties();
    
    InputStream is  =  null;
    
    is = DButil.class.getClassLoader().getResourceAsStream("db.properties");
    
    try {
    
    pop.load(is);
    
    } catch (IOException e) {
    
    // TODO Auto-generated catch block
    
    e.printStackTrace();
    
    }
    
    driver = pop.getProperty("driver");
    
    url = pop.getProperty("url");
    
    username = pop.getProperty("username");
    
    password = pop.getProperty("password");
    
    System.out.println(driver);
    
    Class.forName(driver);
    
    } catch (ClassNotFoundException e) {
    
    // TODO Auto-generated catch block
    
    System.out.println("数据库崩了");
    
    }
    
    }
    
    public static Connection getConn() throws ClassNotFoundException, SQLException{
    
    //加载驱动
    
    Connection conn = DriverManager.getConnection(url, username, password);
    
    conn.setAutoCommit(false);
    
    return conn;
    
    }
    
    }
    
    

    相关文章

      网友评论

          本文标题:DButil

          本文链接:https://www.haomeiwen.com/subject/eakxwqtx.html