用IDEA编写一个较为常用的JDBC程序,配置相关环境及解决出现的问题。
程序如下:
方式1、直接在类中配置数据库相关属性
import java.sql.*;
/**
* 工具类
*/
final class JdbcUtils {
// test是我自己的一个数据库,如果在本机操作的话,localhost:3306可以省略,即直接///的形式
private static String url = "jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String password = "******";
private JdbcUtils() {
}
// 注册驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* 建立连接
*
* @return
* @throws SQLException
*/
public static Connection getConnect() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放资源
*
* @param rs
* @param st
* @param conn
*/
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
方式2、:以配置文件的形式配置数据库相关属性,详细属性参见官方文档:点这里
1.dbcpconfig.properties文件:
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=true
username=root
password=wl968640
#初始化连接
initialSize=200
#最大连接数量,0表示无限制
maxActive=500
#最大空闲连接
maxIdle=6
#最小空闲连接
minIdle=2
#超时等待时间以毫秒为单位 6000毫秒/1000等于60秒
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
2.Utils类:
public final class JdbcUtils {
/**
* 连接池类对象
*/
private static DataSource dataSource;
private JdbcUtils() {
}
// 注册驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
Properties prop = new Properties();
InputStream is = JdbcUtils.class.getClassLoader()
.getResourceAsStream("dbcpconfig.properties");
prop.load(is);
dataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* 建立连接
*
* @return
* @throws SQLException
*/
public static Connection getConnect() throws SQLException {
return dataSource.getConnection();
}
/**
* 释放资源
*
* @param rs
* @param st
* @param conn
*/
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
主类:
import java.sql.*;
public class StandardTemplate {
public static void main(String[] args) throws SQLException {
template();
}
static void template() throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 建立连接
conn = JdbcUtils.getConnect();
// 创建语句
st = conn.createStatement();
// 执行语句
rs = st.executeQuery("select * from tt");
while (rs.next()) {
System.out.println(rs.getObject(1)
+ "\t" + rs.getObject(2)
+ "\t" + rs.getObject(3)
+ "\t" + rs.getObject(4));
}
} finally {
// 释放资源
JdbcUtils.free(rs, st, conn);
}
}
}
但是如果直接运行该程序,会出现Exception in thread “main” java.lang.NoClassDefFoundError: Could not initialize class jdbc.JdbcUtils异常或者Exception in thread “main” java.lang.ClassNotFoundException: com.mysql.jdbc.Driver的异常,这是因为没有导入驱动,导入驱动的过程如下:
1.打开项目设置:在file中的project structure或者项目右上部的设置图标
2.在modules的dependencies下点击+,导入mysql驱动,点击ok即可。
关于驱动的下载:在tool工具管理开启database侧边键或者直接打开database
点击如图的按钮:
点击下载最新版驱动:
下载完成后可以在database中链接你的数据库检验是否可以成功链接:
填写完必要的信息,点击链接测试:
可以看到可以成功链接,点击ok,数据库中的所有表数据都会缓存到database里,可以对其进行相应的操作:
版权声明:本文为qq_40178464原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。