JDBC配置文件properties

package a1;


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class a2 {

public static Connection getconn() throws Exception{
Properties props = new Properties();
Reader is;
try {
is = new FileReader("C:/Users/Administrator/Desktop/a.txt");
props.load(is);
            String a = props.getProperty("co");
            String b = props.getProperty("url");
            String c = props.getProperty("u");
            String d = props.getProperty("p");

try {
Class.forName(a);
System.out.println("驱动链接成功");
Connection conn1 = DriverManager.getConnection(b,c,d);
System.out.println("数据库连接成功");
return conn1;



} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败了");
}




} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;



}








}

猜你喜欢

转载自blog.csdn.net/qq_37069064/article/details/76580970
今日推荐