美文网首页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("打开文件失败");
        }
    }

}

相关文章

  • UiPath连接Azure Sql Server数据库

    UiPath对其他类型数据库的操作UiPath 连接 MySQL 数据库UiPath连接Oracle数据库UiPa...

  • python 教程笔记day10

    Python3 MySQL 数据库连接 数据库连接 创建数据库表 数据库插入操作 数据库查询操作 数据库更新操作 ...

  • 连接数据库其他操作

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

  • sqlalchemy基本操作

    操作说明连接数据库初始化链接信息各数据库连接样例创建连接定义models获取sessionCRUD 操作说明 连接...

  • 6PHP 操作 MySQL 数据库

    [TOC] PHP 操作 MySQL 数据库 连接 MySQL 服务器,连接数据库 进行数据库的操作(CURD) ...

  • Java的JDBC封装

    数据库连接操作类,该类提供创建数据库连接getConnection和release释放资源 数据库的操作类 测试类...

  • MySQL连接数据库js代码封装

    连接数据库js代码:dbase.js 其他需要进行数据库操作的js文件中加载dbase.js模块:

  • Spring-Boot 数据库使用实战(一)

    Spring-Boot连接数据库基本操作 本文采用mysql为例子,其他数据库同类。 使用mysql的同学如出现这...

  • jdbc学习代码

    从基本连接==》配置文件连接==》连接后操作数据库(更改操作和查询操作)封装了 连接、关闭连接的方法

  • 连接数据库(JDBC)

    JDBC:Java DataBase Connection Java数据库连接,用来操作关系型数据库。 连接数据库...

网友评论

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

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