/**
实现功能
1.创建数据库配置文件
2.读取数据库配置文件类
3.创建连接数据库,以及执行sql语句的基类
4.调用基类,对数据库进行查询操作
*/
1.创建数据库配置文件
—文件名 jdbc_mysql.properties
—文件所在位置 如下图:
2.读取数据库配置文件类
package Connection.mysql;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Readconf extends Base {
private final static String path ="./conf/jdbc_mysql.properties";
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) {
}
/*
* 读取配置文件,获取到Driver名称
*
* path,配置文件的地址;演示样式:./conf/jdbc_mysql.properties
* 单词
* driver,字义:驱动,读法:第外我
* */
public static String GetDriver(String path) {
String driver=null;
//文件路径
InputStream is;
try {
//获取配置路径流
is = new FileInputStream(path);
//new 读取配置文件对象
Properties p = new Properties();
try {
//加载配置文件
p.load(is);
//获取驱动名
driver = p.getProperty("driver");
} catch (IOException e) {
System.out.println("没有找到");
}
} catch (FileNotFoundException e1) {
System.out.println("没有找到配置文件");
}
return driver;
}
/*
* 获取数据库地址
* path 配置文件地址
* databasename 数据库名
* */
public static String GetUrl(String path ,String dbname) throws FileNotFoundException{
String url =null;
//文件路径
InputStream is;
try {
//获取配置路径流
is = new FileInputStream(path);
//new 读取配置文件对象
Properties p = new Properties();
try {
//加载配置文件
p.load(is);
//获取驱动名
url = p.getProperty("url");
} catch (IOException e) {
System.out.println("加载文件错误");
}
} catch (FileNotFoundException e1) {
System.out.println("没有找到配置文件");
}
return url+dbname+"?";
}
/*
* 获取数据库用户名
* */
public static String Getuser(String path) throws FileNotFoundException{
String user =null;
//文件路径
InputStream is;
try {
//获取配置路径流
is = new FileInputStream(path);
//new 读取配置文件对象
Properties p = new Properties();
try {
//加载配置文件
p.load(is);
//获取驱动名
user = p.getProperty("user");
} catch (IOException e) {
System.out.println("加载文件错误");
}
} catch (FileNotFoundException e1) {
System.out.println("没有找到配置文件");
}
return user;
}
/*
* 获取数据库密码
* */
public static String GetPass(String path) throws FileNotFoundException{
String pass =null;
//文件路径
InputStream is;
try {
//获取配置路径流
is = new FileInputStream(path);
//new 读取配置文件对象
Properties p = new Properties();
try {
//加载配置文件
p.load(is);
//获取驱动名
pass = p.getProperty("pass");
} catch (IOException e) {
System.out.println("加载文件错误");
}
} catch (FileNotFoundException e1) {
System.out.println("没有找到配置文件");
}
return pass;
}
}
3.创建连接数据库,以及执行sql语句的基类
package Connection.mysql;
import java.io.FileNotFoundException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class Jdbc_Base extends Readconf{
private final static String path ="./conf/jdbc_mysql.properties"; //配置文件路径
public static Connection conn = null; //连接服务器对象
public static PreparedStatement ps =null; //执行sql 对象
public static ResultSet rs =null; //结果集对象
/**
* @param args
*/
public static void main(String[] args) {
// boolean falg = Connection_db("demo");
// System.out.println(falg);
}
/*
* 连接数据库
* dbname : 要连接的数据库名
* */
public static boolean Connection_db(String dbname) {
boolean falg = false;
/*
* 准备参数
* driver:驱动名
*
* */
String driver = Readconf.GetDriver(path);
String url =null;
String user =null;
String pass =null;
try {
url = Readconf.GetUrl(path, dbname);
user = Readconf.Getuser(path);
pass = Readconf.GetPass(path);
} catch (FileNotFoundException e) {
System.out.println("没有找到文件,错误在Readconf");
}
try {
//1.加载驱动
Class.forName(driver);
System.out.println("加载驱动成功");
//2.连接数据库
conn = (Connection) DriverManager.getConnection(url, user, pass);
//判断数据是否关闭着
boolean closed = conn.isClosed();
if(!closed){
System.out.println("数据库连接成功");
falg=true;
}else{
System.out.println("数据库连接失败");
}
} catch (ClassNotFoundException e) {
System.out.println("没有找到文件,错误在Readconf");
} catch (SQLException e) {
System.out.println("连接数据库失败");
}finally{
// try {
// conn.close();
// } catch (SQLException e) {
// System.out.println("关闭---Connection---错误");
// }
}
return falg;
}
/*
* 执行sql,返回结果集
* */
public static ResultSet execute_sql(String dbname,String sql){
boolean falg = Connection_db(dbname);
if(falg){
try {
//预加载sql语句
ps = (PreparedStatement) conn.prepareStatement(sql);
//执行sql, 且获取返回值
rs = ps.executeQuery();
} catch (SQLException e) {
System.out.println("运行sql失败:"+sql);
}finally{
// try {
// ps.close();
// } catch (SQLException e) {
// System.out.println("关闭---PreparedStatement---错误");
// }
}
}else{
System.out.println("没有连接上数据库");
}
return rs;
}
}
4.调用基类,对数据库进行查询操作
package Connection.mysql;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Demo1 extends Jdbc_Base{
/**
* @param args
*/
public static void main(String[] args) {
select();
}
//执行查询
public static void select(){
//sql 语句 查询 id=1 的那一行的数据
String sql ="select * from user where id = '1'";
System.out.println(sql);
ResultSet rs = Jdbc_Base.execute_sql("demo", sql);
try {
//测试部分
if(rs.next()){
//通过字段获取到 对应的值
String username = rs.getString("username");
//打印数据库获取到的值
System.out.println("---------");
System.out.println("user:"+username);
System.out.println("---------");
}else{
System.out.println("kong");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关闭操作
rs.close();
Jdbc_Base.ps.close();
Jdbc_Base.conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}