package com.qf.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestEmp2 {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnect(){
String url = "jdbc:mysql://localhost:3306/db2301";
String root = "root";
String password = "admin";
Connection c = null;
try {
c = DriverManager.getConnection(url,root,password);
} catch (SQLException e) {
e.printStackTrace();
}
return c;
}
public static void main(String[] args) {
Connection c = getConnect();
Statement statement = null;
try {
statement = c.createStatement();
String sql = "delete from emp where ename = '张四'";
String sql1 = "update emp set sal = 9999 where ename = '巴同学'";
int a1 = statement.executeUpdate(sql);
if (a1 > 0){
System.out.println("删除成功");
}
int a2 = statement.executeUpdate(sql1);
if (a2 > 0){
System.out.println("修改成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
statement.close();
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
java jdbc连接数据库实现增删改查
最新推荐文章于 2024-05-11 16:42:02 发布