Insert 类 调用 JdbcUtil类时出现异常,但代码没有红叉,请大神指教!
//第一个类
package com.jdbc.util;
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 JdbcUtil {
//数据库的连接的URL
private static String url = null;
//数据库用户名
private static String user = null;
//数据库密码
private static String password = null;
//驱动程序类
private static String driverClass = null ;
public static void main(String[] args) {
//static {
//注册驱动
try {
//创建Properties对象,接入jdbc.properties配置文件
Properties prop = new Properties();
//使用类路径方式读取配置文件
InputStream in = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
//加载文件
prop.load(in);
//读取配置文件的内容
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
driverClass = prop.getProperty("driverClass");
Class.forName(driverClass);
System.out.println(url);
System.out.println(user);
System.out.println(password);
System.out.println(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接方法
public static Connection getConn() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
//释放资源方法
public static void close(ResultSet rs,Statement stmt,Connection conn) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
//jdbc.properties配置文件:
url=jdbc:mysql://localhost:3306/test
user=root
password=root
driverClass=com.mysql.jdbc.Driver
//第二个类
package com.crud.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.jdbc.util.JdbcUtil;
public class Insert {
public static void main(String[] args) {
test();
}
public static void test() {
Connection conn = null;
PreparedStatement stmt =null;
ResultSet rs = null ;
try {
//获取连接
conn = JdbcUtil.getConn();
String sql = "INSERT INTO student(id,name,age) VALUES(?,?,?)";
//创建prepareStatement对象
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setInt(1, 7);
stmt.setString(2, "奥巴马");
stmt.setInt(3, 22);
//发送参数到数据库
rs = stmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭资源
JdbcUtil.close(rs, stmt, conn);
}
}
}
删 public static void main(String[] args) {
放开 static
over!
更新(insert、update、delete)用 executeUpdate
算了,给你代码吧,自己理解
package com.jdbc.util;
//import java.io.IOException;
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 JdbcUtil
{
// 数据库的连接的URL
private static String url = null;
// 数据库用户名
private static String user = null;
// 数据库密码
private static String password = null;
// 驱动程序类
private static String driverClass = null;
public static void main(String[] args)
{
System.out.println(getConn());
}
/**
* 只注册一次,静态代码块
*/
static
{
// 注册驱动
try
{
// 创建Properties对象,接入jdbc.properties配置文件
Properties prop = new Properties();
// 使用类路径方式读取配置文件
InputStream in = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
// 加载文件
prop.load(in);
// 读取配置文件的内容
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
driverClass = prop.getProperty("driverClass");
Class.forName(driverClass);
System.out.println(url);
System.out.println(user);
System.out.println(password);
System.out.println(driverClass);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 获取连接方法
*/
public static Connection getConn()
{
try
{
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 释放资源方法
*/
public static void close(ResultSet rs, Statement stmt, Connection conn)
{
if (rs != null)
{
try
{
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
if (stmt != null)
{
try
{
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
package com.crud.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.jdbc.util.JdbcUtil;
public class Insert
{
public static void main(String[] args)
{
test();
}
public static void test()
{
Connection conn = null;
PreparedStatement stmt = null;
try
{
// 获取连接
conn = JdbcUtil.getConn();
String sql = "INSERT INTO student(id,name,age) VALUES(?,?,?)";
// 创建prepareStatement对象
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setInt(1, 7);
stmt.setString(2, "奥巴马");
stmt.setInt(3, 22);
// 发送参数到数据库
stmt.executeUpdate();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
// 关闭资源
JdbcUtil.close(null, stmt, conn);
}
}
}
InputStream in = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
改成
InputStream in = JdbcUtil.class.getResourceAsStream("jdbc.properties");
试试。
给url,user,password赋值的方法没有执行
如描述所示:获取的 url 应该是null
1.首先查看配置文件是否获取到。
2.查看配置文件中是否有url=xxxxx
例如我是如此获取src/main/resources/下的配置文件的
String path = this.getClass().getResource("/").getPath() + File.separator + "config.properties";