jdbc 连接后的操作主要有两个:数据操作和数据查询;
数据操作
这里以删除操作为例;
package p1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyConnection {
public static boolean update(String sql) {
Connection connection=null;
Statement statement=null;
try {
Class.forName("com.mysql.jdbc.Driver"); //1、加载驱动
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1/keeper", "root","root");
//2、创建连接
statement=connection.createStatement(); //3、创建命令语句对象
int affect=statement.executeUpdate(sql); //4、执行命令,获取结果
return affect>0;
} catch (Exception e) {
e.printStackTrace();
}finally { //5、释放资源
if (statement!=null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
return false;
}
public static void main(String[] args) {
String sql="delete from Goods where id