美文网首页JAVA学习过程
连接数据库其他操作

连接数据库其他操作

作者: _String_ | 来源:发表于2017-09-24 15:42 被阅读0次

    上次提到过可以通过参数的方式将要连接的数据库url、用户名及密码等通过参数的方式传给连接函数,下面给出具体测试代码

    package Datacon;
    
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import com.mysql.jdbc.Connection;
    
    public class Dataconn {
        public String driver="com.mysql.jdbc.Driver";
        private String url = "jdbc:mysql://127.0.0.1:3306/test";
        private String user ="root";
        private String passwd = "root@2017";
        
        //声明默认连接函数
        public Connection  defDataConn() throws ClassNotFoundException, SQLException{
            
            Connection connDB = null;  //初始化返回null
            //加载驱动类
            try{
            Class.forName(driver);
            }catch(ClassNotFoundException e){
                e.printStackTrace();
                System.out.println("未找到指定类");
            }
            try{
            connDB = (Connection) DriverManager.getConnection(url, user, passwd);
            }catch(SQLException
                    e){
                e.printStackTrace();
                System.out.println("数据库连接失败");
            }
            return connDB;
            
        }
        public Connection setConnDB(){
            Connection connDB = null;
            try{
                Class.forName(driver);
                }catch(ClassNotFoundException e){
                    e.printStackTrace();
                    System.out.println("未找到指定类");
                }
                try{
                connDB = (Connection) DriverManager.getConnection(url, user, passwd);
                }catch(SQLException
                        e){
                    e.printStackTrace();
                    System.out.println("数据库连接失败");
                }
                System.out.println("===================================================");
                System.out.println("=====================自定义函数========================");
                System.out.println("===================================================");
                return connDB;
            
        }
        public Dataconn(String driver, String url, String user, String passwd) {
            super();
            this.driver = driver;
            this.url = url;
            this.user = user;
            this.passwd = passwd;
        }
    
    }
    

    除以上方法连接数据库jdbc还提供通过读取配置文件的方式连接数据库,读取配置文件通过Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集。需要注意的是配置文件的存放位置为class path目录。

    properties配置文件

    新建配置文件,通过右击src文件夹选择new-other,选择file类型

    选择file类型

    输入文件名及扩展名点击保存即可。
    点击add添加对应配置文件信息。
    添加完成点击保存即可

    输入内容

    通过Properties类实例化,通过x.load()方式导入文件内容。如下:

    prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
    读取配置文件详细代码如下:

    package Datacon;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    import com.mysql.jdbc.Connection;
    
    public class Dataconn {
        public String driver="com.mysql.jdbc.Driver";
        private String url = "jdbc:mysql://127.0.0.1:3306/test";
        private String user ="root";
        private String passwd = "root@2017";
        
        public Dataconn(){
            
        }
        //声明默认连接函数
        public Connection  defDataConn() throws ClassNotFoundException, SQLException{
            
            Connection connDB = null;  //初始化返回null
            //加载驱动类
            try{
            Class.forName(driver);
            }catch(ClassNotFoundException e){
                e.printStackTrace();
                System.out.println("未找到指定类");
            }
            try{
            connDB = (Connection) DriverManager.getConnection(url, user, passwd);
            }catch(SQLException
                    e){
                e.printStackTrace();
                System.out.println("数据库连接失败");
            }
            return connDB;
            
        }
        public Connection setConnDB(){
            Connection connDB = null;
            try{
                Class.forName(driver);
                }catch(ClassNotFoundException e){
                    e.printStackTrace();
                    System.out.println("未找到指定类");
                }
                try{
                connDB = (Connection) DriverManager.getConnection(url, user, passwd);
                }catch(SQLException
                        e){
                    e.printStackTrace();
                    System.out.println("数据库连接失败");
                }
                System.out.println("===================================================");
                System.out.println("=====================自定义函数========================");
                System.out.println("===================================================");
                return connDB;
            
        }
        public Dataconn(String driver, String url, String user, String passwd) {
            super();
            this.driver = driver;
            this.url = url;
            this.user = user;
            this.passwd = passwd;
        }
        public Connection openFileconn() throws IOException, ClassNotFoundException, SQLException{
            Connection dataConn = null;
            
            Properties prop = new Properties();
            try{
                prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));  //读入配置文件 
                this.driver=prop.getProperty("driver"); //获取配置文件中相应键值
                this.url = prop.getProperty("url");
                this.user = prop.getProperty("user");
                this.passwd = prop.getProperty("password");
            }catch(IOException e){
                e.printStackTrace();
            }
            System.out.println("config file :"+this.driver+this.url+this.user+this.passwd);
            try{
                Class.forName(driver);
            }catch(ClassNotFoundException e){
                e.printStackTrace();
                System.out.println("为找到该java驱动");
            }
            try{
            dataConn = (Connection) DriverManager.getConnection(url, user, passwd);
            }catch(SQLException e){
                e.printStackTrace();
                System.out.println("连接失败");
            }
            return dataConn;
        }
    
    }
    

    方法测试代码如下:

    package Datacon;
    
    import java.io.IOException;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;
    
    import org.junit.Test;
    
    import com.mysql.jdbc.Connection;
    
    public class testRun { 
        
        @Test
        public void runDef() throws ClassNotFoundException, SQLException{
            Dataconn dataconn = new Dataconn();
            System.out.print(dataconn);
            try{
            Connection connDB= dataconn.defDataConn();  //调用数据库连接函数
            Statement stat = connDB.createStatement();  //初始化statement对象连接数据库
            String SQL = "select * from users";
            ResultSet res = stat.executeQuery(SQL);  //查询指定数据
            //遍历数据集合
            while(res.next()){
                String name = res.getNString(2);
                System.out.println("查询内容为:"+name);
            }
            }catch(SQLException e){
                e.printStackTrace();
                System.out.println("连接失败");
            }
            
        } 
        @Test
        public void runSet(){
            Dataconn dataconn = new Dataconn("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/test", "root", "root@2017");
            System.out.print(dataconn);
            try{
            Connection connDB= dataconn.setConnDB();  //调用数据库连接函数
            Statement stat = connDB.createStatement();  //初始化statement对象连接数据库
            String SQL = "select * from users";
            ResultSet res = stat.executeQuery(SQL);  //查询指定数据
            //遍历数据集合
            while(res.next()){
                String name = res.getNString(2);
                System.out.println("查询内容为:"+name);
            }
            connDB.close();
            }catch(SQLException e){
                e.printStackTrace();
                System.out.println("连接失败");
            }
                
        }
        @Test
        public void openFileconn() throws IOException, ClassNotFoundException, SQLException{
            Dataconn dataconn = new Dataconn();
            System.out.print(dataconn);
            try{
            Connection connDB= dataconn.openFileconn();  //调用数据库连接函数
            Statement stat = connDB.createStatement();  //初始化statement对象连接数据库
            String SQL = "select * from users";
            ResultSet res = stat.executeQuery(SQL);  //查询指定数据
            //遍历数据集合
            while(res.next()){
                String name = res.getNString(2);
                System.out.println("查询内容为:"+name);
            }
            }catch(IOException e){
                e.printStackTrace();
                System.out.println("打开文件失败");
            }
        }
    
    }
    

    相关文章

      网友评论

        本文标题:连接数据库其他操作

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