package JDBC;
import java.sql.*;
public class JDBCDemon1 {
public static void JDBCStep(){
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// DriverManger连接mysql时路径格式:jdbc:mysql://<host>:<port>/<dbname> msql端口号通常为3306
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/scott?user=root&password=100521");
/*
通过Connection创建Statement来执行SQL语句
*/
Statement statement = connection.createStatement();
/*
execute用于执行DDL语句返回的结果为是否执行成功
executeQuery用于执行DQL(select)语句,返回一个结果集resultSet
executeUpdate用于执行DML(增删改操纵)返回int值为影响数据库多少条数据
*/
/*
通过Statment执行SQL语句,查询语句emp表中的信息:select empno,ename,sal,deptno from emp;
并且输出sql,用于检查sql语句是否正确
*/
String sql = "select empno,ename,sal,deptno from emp";
System.out.println(sql);
/*
使用executeQuery执行DQL语句,且查询后会得到一个结果集
*/
ResultSet resultSet = statement.executeQuery(sql);
/*
不能在此时进行关闭,查询的结果集在服务器中,并不在客户端;
resultSet是一个代理(是一个结果集,但不是全部都载到本地的)
我们通过resultSet的next()方法获取下一条记录时,
resultSet会发送请求到服务器端获取数据,若连接诶关闭,则会抛出异常
connection.close();
*/
while (resultSet.next()){
int empno = resultSet.getInt("empno");
String empname = resultSet.getString("ename");
Double sal = resultSet.getDouble("sal");
int deptno = resultSet.getInt("deptno");
System.out.println("empno:"+empno+",ename:"+empname+",sal:"+sal+",deptno:"+deptno);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭连接
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JDBCStep();
}
}
#配置文件
driver=com.mysql,jdbc.Driver
user=root
pwd=******
url=jdbc:mysql://localhost:3306/scott
package JDBC;
/**
* 测试配置文件的读取
* config.properties
*/
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class JDBCDemon2 {
public static void main(String[] args) {
Connection connection = null;
try {
//java.util.Properties
/*
* Properties用于读取properties文件
* 使用该类可以以类似Map的形式读取配置文件中的内容
* properties文件总的内容格式类似:
* user=root
* 那么等号左边就是key,等号右边就是value
*/
Properties properties = new Properties();
/*
* 使用Properties读取配置文件
* */
FileInputStream fileInputStream = new FileInputStream("./src/JDBC/config.properties");
/*
* 当通过Properties读取文件后,
* 那么这个流依然保持打开状态,
* 我们应该自行对其进行关闭
* */
properties.load(fileInputStream);
System.out.println("成功加载完配置文件");
/*
* 当加载完毕后,就可以根据文本文件中
* 等号左边的内容(key)来获取
* 等号右边的值(value)
* 可以变相的吧Properties看做一个Map
* */
String driver = properties.getProperty("driver").trim();
String url = properties.getProperty("url").trim();
String pwd = properties.getProperty("pwd").trim();
String user = properties.getProperty("user").trim();
System.out.println("Driver:"+driver);
System.out.println("user:"+user);
System.out.println("url:"+url);
System.out.println("pwd:"+pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package JDBC;
/**
* 使用配置文件来配置JDBC连接数据库
* 该类用来管理数据库的连接
*/
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JDBCDemon3 {
//数据库驱动
private static String driver;
//连接数据库的路径
private static String url;
//连接数据库的用户名
private static String user;
//连接数据库的密码
private static String pwd;
//静态块:类被虚拟机加载时执行一次
static {
try {
//读取配置文件
Properties properties = new Properties();
//更加推荐的相对路径写法
InputStream inputStream = JDBCDemon3.class.getClassLoader()
.getResourceAsStream("JDBC/config.properties");
properties.load(inputStream);
driver = properties.getProperty("driver").trim();
url = properties.getProperty("url").trim();
pwd = properties.getProperty("pwd").trim();
user = properties.getProperty("user").trim();
System.out.println("Driver:" + driver);
System.out.println("user:" + user);
System.out.println("url:" + url);
System.out.println("pwd:" + pwd);
inputStream.close();
//获取驱动
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取一个连接
*/
public static Connection getConnection() throws Exception{
try {
/*
* 通过DriverManager创建一个数据库连接并返回
* */
DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
e.printStackTrace();
//通知调用者,创建连接出错
throw e;
}
return DriverManager.getConnection(url,user,pwd);
}
/**
* 关闭连接
*/
public static void closeConnection(Connection connection){
try{
if(connection != null){
connection.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 获取连接并执行sql
*/
public static void main(String[] args) {
try{
Connection connection = JDBCDemon3.getConnection();
System.out.println("数据库已经连接");
Statement statement = connection.createStatement();
String sql = "select * from emp";
System.out.println(sql);
//执行sql,得到结果集
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
int empno = resultSet.getInt("empno");
String ename = resultSet.getString("ename");
int deptno = resultSet.getInt("deptno");
double sal = resultSet.getDouble("sal");
System.out.println("empno:"+empno+" ename:"+ename+" deptno:"+deptno+" sal:"+sal);
}
//当结果集使用完毕后就应该关闭,释放资源
//但是若Statment关闭了,那么resultSet也会自动关闭
resultSet.close();
//当不再使用Statment执行其他sql时,应该及时关闭Statment以释放JDBC与数据库的资源连接
statement.close();
//使用后关闭连接
closeConnection(connection);
}catch (Exception e){
e.printStackTrace();
}
}
}
package JDBC;
/**
* 使用配置文件来配置JDBC连接数据库
* 该类用来管理数据库的连接
*/
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JDBCDemon4 {
//数据库驱动
private static String driver;
//连接数据库的路径
private static String url;
//连接数据库的用户名
private static String user;
//连接数据库的密码
private static String pwd;
//用于管理不同线程所获取的连接
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
//静态块:类被虚拟机加载时执行一次
static {
try {
//读取配置文件
Properties properties = new Properties();
//更加推荐的相对路径写法
InputStream inputStream = JDBCDemon4.class.getClassLoader()
.getResourceAsStream("JDBC/config.properties");
properties.load(inputStream);
driver = properties.getProperty("driver").trim();
url = properties.getProperty("url").trim();
pwd = properties.getProperty("pwd").trim();
user = properties.getProperty("user").trim();
System.out.println("Driver:" + driver);
System.out.println("user:" + user);
System.out.println("url:" + url);
System.out.println("pwd:" + pwd);
inputStream.close();
//获取驱动
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取一个连接
*/
public static Connection getConnection() throws Exception{
try {
/*
* 通过DriverManager创建一个数据库连接并返回
* */
Connection connection = DriverManager.getConnection(url,user,pwd);
/*
* ThreadLocal的set方法会将当前线程作为key
* 并将给定的值作为value存入内部的map中保存
* */
threadLocal.set(connection);
return connection;
} catch (Exception e) {
e.printStackTrace();
//通知调用者,创建连接出错
throw e;
}
}
/**
* 关闭给定的连接
*/
public static void closeConnection(){
try{
//在ThreadLocal中获取connection即(value值)
Connection connection = threadLocal.get();
if(connection != null){
connection.close();
//删除value值,防止污染
threadLocal.remove();
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 获取连接并执行sql
*/
public static void main(String[] args) {
try{
Connection connection = JDBCDemon4.getConnection();
System.out.println("数据库已经连接");
Statement statement = connection.createStatement();
String sql = "select * from emp";
System.out.println(sql);
//执行sql,得到结果集
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
int empno = resultSet.getInt("empno");
String ename = resultSet.getString("ename");
int deptno = resultSet.getInt("deptno");
double sal = resultSet.getDouble("sal");
System.out.println("empno:"+empno+" ename:"+ename+" deptno:"+deptno+" sal:"+sal);
}
//当结果集使用完毕后就应该关闭,释放资源
//但是若Statment关闭了,那么resultSet也会自动关闭
resultSet.close();
//当不再使用Statment执行其他sql时,应该及时关闭Statment以释放JDBC与数据库的资源连接
statement.close();
//使用后关闭连接
closeConnection();
}catch (Exception e){
e.printStackTrace();
}
}
}