import java.sql.*;
public class JDBCConnection {
private static final String URL="jdbc:mysql://localhost:3306/db";
private static final String USERNAME = "root";
private static final String PWD = "root";
public static void update(){//增删改
Connection connection = null;
Statement stmt = null;
try {
//导入驱动 加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//与数据库建立连接
connection = DriverManager.getConnection(URL,USERNAME,PWD);
//发送sql,执行(增删改、查)
stmt = connection.createStatement();
String sql = "delete from student where id=1";
int count = stmt.executeUpdate(sql);
//处理结果
if (count>0){
System.out.println("操作成功!");
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if (connection!=null)connection.close();
if (stmt!=null)stmt.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
update();
}
}
记录一下菜鸟的成长-idea jdbc连接和操作数据库首次成功!
最新推荐文章于 2020-11-13 23:16:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)