Java連接Mysql:通過配置文件


轉載自:https://blog.csdn.net/mrw456/article/details/49718235

 

1、導入數據庫驅動jar包,與及編寫數據庫配置文件(mysql.properties),mysql.properties放在項目的src目錄下[本例使用的數據庫為Mysql5.5,數據庫驅動jar包為mysql-connector-java-5.0.8-bin.jar]
mysql.properties:

name=com.mysql.jdbc.Driver                    //數據庫驅動名稱
url=jdbc:mysql://127.0.0.1:3306/dormitory //數據庫連接url
user=root                                               //數據庫連接用戶名
password=123456                                 //數據庫連接密碼

2、編寫mysql.properties讀取屬性類Mysqlread.java,負責讀取mysql.properties中的屬性名稱
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class Mysqlread {
    public static final String [] message=readurl();
    private static String[] readurl() {
        Properties prop=new Properties();
        String [] message=new String[4];
        int i=0;
        try {
            InputStream in=new BufferedInputStream(new FileInputStream("src/mysql.properties"));
            prop.load(in);
            message[0]=prop.getProperty("name");
            message[1]=prop.getProperty("url");
            message[2]=prop.getProperty("user");
            message[3]=prop.getProperty("password");
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return message;
    }
}

3、編寫數據庫連接類MysqlOperation.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MysqlOperation {
    private static final String[] mysqlmessage=Mysqlread.message;
    public static Connection getConnection()
    {
        Connection conn=null;
        try {
            Class.forName(mysqlmessage[0]);
            conn=DriverManager.getConnection(mysqlmessage[1],mysqlmessage[2],mysqlmessage[3]);
        } catch (ClassNotFoundException e) {
            System.out.println("驅動類庫不能發現");
        } catch (SQLException e) {
            System.out.println("SQL異常");
        }
        return conn;
    }
    public static void close(Connection conn){
        if(conn!=null)
        {
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("SQL異常");
            }
        }
    }
    public static void close(Statement statement){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement=null;
        }
    }
    public static void close(ResultSet rs)
    {
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs=null;
        }
    }
}

4、編寫一個測試類測試上述代碼是否成功
import static org.junit.Assert.*;
import java.sql.Connection;
import org.junit.Test;
import com.mrw.util.MysqlOperation;
public class TestPropertisread {
    @Test
    public void test() {
        Connection conn=MysqlOperation.getConnection();
        System.out.println(conn);
        MysqlOperation.close(conn);
    }
}

運行結果為:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM