多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ package JDBC; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * account表 添加一条记录 insert 语句 */ public class JDBCDemo2 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.定义sql String sql = "insert into account(name,balance) values('王五',256.58)"; //3.获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql:///db1?serverTimezone=UTC", "root", "admin123"); //4.获取执行sql的对象Statement stmt = conn.createStatement(); //5.执行sql int count = stmt.executeUpdate(sql); //6.处理结果 System.out.println(count); if (count > 0) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { //7.释放资源 //避免空指针异常 if (stmt != null) { try { stmt.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } } } ~~~