Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。
JDBC也是Sun Microsystems的商标。我们通常说的JDBC是面向关系型数据库的。
为了简化 开发人员的(对数据的统一)操作,提供了一个java操作数据库规范,对于开发人员来说,我们只需要掌握JDBC接口的操作即可;
<!-- 数据库驱动 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
className=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/wangdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
user=admin
password=Admin@123
public class JdbcDemo {
public static void main(String[] args) {
PreparedStatement statement = null;
Connection connection = null;
ResultSet set = null;
try {
// 1、加载配置文件
Properties props = new Properties();
props.load(new FileInputStream("properties.properties"));
String className = props.getProperty("className");
String url = props.getProperty("url");
String user = props.getProperty("user");
String password = props.getProperty("password");
// 2、加载驱动
Class.forName(className);
// 3、获取连接
connection = DriverManager.getConnection(url, user, password);
// 4、sql语句 可以通过
String sql =
"select t.Id, t.`NAME`,t.DEPT_NAME,t.ID_NO,t.CreateDate from user_info t where id=?";
// 5、获得Satement执行sql语句 或者 获得PrepareStatement执行sql语句
statement = connection.prepareStatement(sql);
// 6、设置参数,一个?对应一个值,不能有其他的关键字
statement.setObject(1, 1);
// 7、执行语句 executeUpdate:增删改 executeQuery:执行查询操作
set = statement.executeQuery();
// 8、从ResultSet中将数据传入到我们自己定义的对象中
while (set.next()) {
int id = set.getInt("id");
String name = set.getString("name");
String idNo = set.getString("ID_NO");
Date date = set.getDate("CreateDate");
Timestamp time = set.getTimestamp("CreateDate");
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("idNo:" + idNo);
System.out.println("date:" + date);
System.out.println("time:" + time);
System.out.println("---------------");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 9、关闭连接
try {
if (set != null) {
set.close();
}
Objects.requireNonNull(connection).close();
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
连接参数增加
className=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/wangdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&&useServerPrepStmts=true
user=admin
pass
修改 mysql.ini
[mysqld]
## 增加日志监控
log-output=FILE
general-log=1
general_log_file="D:\java\mysql\phpstudy_pro\Extensions\MySQL5.7.26\mysql.log"
slow-query-log=1
slow_query_log_file="D:\java\mysql\phpstudy_pro\Extensions\MySQL5.7.26\mysql_slow.log"
long_query_time=2
try {
// ============开启事务==========
conn.setAutoCommit(false);
//5. 执行sql
int count1 = stmt.executeUpdate(sql1);//受影响的行数
//6. 处理结果
System.out.println(count1);
int i = 3/0;
//5. 执行sql
int count2 = stmt.executeUpdate(sql2);//受影响的行数
//6. 处理结果
System.out.println(count2);
// ============提交事务==========
//程序运行到此处,说明没有出现任何问题,则需求提交事务
conn.commit();
} catch (Exception e) {
// ============回滚事务==========
//程序在出现异常时会执行到这个地方,此时就需要回滚事务
conn.rollback();
e.printStackTrace();
}
配置文件
driver-class-name=com.mysql.cj.jdbc.Driver
username=admin
password=Admin@123
url=jdbc:mysql://localhost:3306/wangdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&&useServerPrepStmts=true
initialSize=5
minIdle=5
maxActive=20
程序
public class DruidDemo {
public static void main(String[] args) throws Exception {
PreparedStatement statement = null;
Connection connection = null;
ResultSet set = null;
// 1、导入jar包
// 2、 加载配置文件,路径根据情况自己写
String propsPath = System.getProperty("user.dir") + "\day02\src\druid.properties";
Properties prop = new Properties();
prop.load(new FileInputStream(propsPath));
// 3、获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
// 4、获取连接
connection = dataSource.getConnection();
// 5、编写sql
String sql =
"select t.Id, t.NAME
,t.DEPT_NAME,t.ID_NO,t.CreateDate from user_info t where id=?";
// 6、获取PrepairStatement
statement = connection.prepareStatement(sql);
// 7、执行sql语句
statement.setObject(1, 1);
set = statement.executeQuery();
// 8、遍历
while (set.next()) {
int id = set.getInt("id");
String name = set.getString("name");
String idNo = set.getString("ID_NO");
Date date = set.getDate("CreateDate");
Timestamp time = set.getTimestamp("CreateDate");
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("idNo:" + idNo);
System.out.println("date:" + date);
System.out.println("time:" + time);
System.out.println("---------------");
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章