抽取JDBC工具类之后无法运行
来源:4-2 JDBC的工具类的抽取二
TT拖鞋
2020-02-07 15:34:49
package com.imooc.jdbc.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
/**
* JDBC的工具类
* @throws ClassNotFoundException
*/
private static final String driverclass;
private static final String url;
private static final String username;
private static final String password;
static {
//加载属性文件并解析
Properties properties = new Properties();
InputStream is =JDBCUtils.class.getClassLoader().getResourceAsStream("JDBCUtils.properties");
try {
properties.load(is);
} catch (Exception e) {
// TODO: handle exception
}
driverclass = properties.getProperty("driverClass");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
/**
* 注册驱动
* @throws ClassNotFoundException
*/
public static void loadDriver() throws ClassNotFoundException {
Class.forName(driverclass);
}
/**
* 获得链接
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException, ClassNotFoundException {
loadDriver();
Connection connection= DriverManager.getConnection(url, username, password);
return connection;
}
/**
* 资源的释放
*/
public static void release(Statement statement,Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection=null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
statement=null;
}
}
public static void release(Statement statement,Connection connection,ResultSet rSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection=null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
statement=null;
}
if (rSet != null) {
try {
rSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rSet=null;
}
}
}
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///jdbctest?characterEncoding=utf-8
username=root
psaaword=123456
package com.imooc.jdbc.demo1;
import java.sql.Connection;
import java.sql.Statement;
import org.junit.Test;
import com.imooc.jdbc.utils.JDBCUtils;
public class JDBCDemo3 {
@Test
public void demo1() {
Connection connection=null;
Statement statement=null;
try {
connection=JDBCUtils.getConnection();
statement=connection.createStatement();
String sql = "insert into goods values(null,'魔方',100,'三阶魔方')";
int i = statement.executeUpdate(sql);
if (i > 0) {
System.out.println("保存成功");
}
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtils.release(statement, connection);
}
}
}
1回答
同学你好,查看贴出代码,JDBCUtils.properties中密码单词书写有误,是password而不是psaaword,这个错误会导致工具类中获取密码错误,修改后再来尝试一下。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题