老师帮忙看看哪错了,麻烦啦
package com.jdbc_demos.demo1;
import java.sql.Connection;
import java.sql.Statement;
import org.junit.Test;
import com.jdbc_demos.utils.JDBCUtils;
public class Demo3 {
/**
* 保存记录
*/
@Test
public void demo1() {
Connection conn = null;
Statement stmt = null;
//获得连接
try {
//获得连接
conn = JDBCUtils.getConnection();
//创建执行sql的对象
stmt = conn.createStatement();
//编写sql
String sql = "insert stu values(null,'vehe',112)";
int num = stmt.executeUpdate(sql);
if(num > 0) {
System.out.println("保存成功!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(stmt, conn);
}
}
}
============================================================
package com.jdbc_demos.utils;
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;
/**
* JDBC工具类
* @author 10570
*
*/
public class JDBCUtils {
private static final String diverClass;
private static final String url;
private static final String username;
private static final String password;
static {
//加载属性文件并分析
Properties props = new Properties();
//如何获取属性文件的输入流?
//通常情况下使用类的加载器的方式进行获取
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properies");
try {
props.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
diverClass = props.getProperty("diverClass");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
}
/**
* 注册驱动的方法
* @throws ClassNotFoundException
*
*/
public static void loadDriver() throws ClassNotFoundException {
Class.forName(diverClass);
}
/**
* 获得连接方法
* @throws Exception
*/
public static Connection getConnection() throws Exception {
loadDriver();
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
/**
* 释放资源
*/
public static void release(Statement stmt,Connection conn) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = null;
}
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
}
============================================================
diverClass = com.mysql.cj.jdbc.Driver
url = jdbc:mysql:///king?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
username = root
password = 19980915
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星