package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class jdbcDemo1 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//定义sql语句
String sql="insert into account values (null,'王五',3000)";
//连接数据库,获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql:///db1","root","root");
//获取sql对象 Statement
stmt = conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println(count);
if (count>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
}
posted on   凛至  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~



点击右上角即可分享
微信分享提示