废话不多说,直接上操作图,操作起来理解会更彻底
1、myeclipse中项目结构,先在web-inf-lib下导入jar包,注意Web App Libraries这个目录,下面会有用到的地方
2、连接语句
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
public class BaseDao {
//第一步:先设置四个常量(url数据库地址,username数据库用户名,password数据库密码,driver数据库驱动)
private static final String URL = "jdbc:mysql://127.0.0.1:3306/petshop?characterEncoding=utf-8";
private static final String USERNAME = "root";
private static final String PASSWORD = "ok";
private static final String DRIVER = "com.mysql.jdbc.Driver";//Web App Libraries -- mysql-connector.jar--下的文件名
//1.加载驱动,用静态块
//jdbc是java提供给各大数据库开发商的一些接口(规则),而驱动就是这些规则的具体实现方法
static{
try {
Class.forName(DRIVER);//要求JVM查找并加载指定的类,也就是注册mysql实现的驱动类
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//2.获取数据库连接,通过在DriverManager驱动管理类中设置参数
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
//3.创建预编译执行对象PreparedStatement
public PreparedStatement createPreparedStatement(Connection conn,
String sql,Object... params) throws SQLException{
//从当前的conn对象中,获取执行对象,把预编译sql语句放入其中
PreparedStatement pstmt = conn.prepareStatement(sql);
//如果预编译sql中有占位符,则表示要赋值
if(params!=null && params.length>0){
//通过遍历,将params对象集合中的元素一一对应付给占位符
for(int i=1;i<=params.length;i++){
pstmt.setObject(i, params[i-1]);
}
}
return pstmt;//带有sql语句的预编译执行对象
}
//4.创建公共DML执行方法
public int executeDML(String sql,Object... params){
int result = 0;//用于返回受影响的行数来判断是否执行成功
//创建两个数据库对象
Connection conn = null;//数据库连接对象
PreparedStatement pstmt = null;//预编译执行对象,接收
try {
conn = this.getConnection();
pstmt = this.createPreparedStatement(conn, sql, params);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn,pstmt,null);
}
return result;
}
//5.释放公共资源方法
void closeAll(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3、测试类中的代码,实现简单的增删改查
package utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class TestDML {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请选择您要执行的操作\\n1.执行 2.删除 3.修改4.查所有");
TestDML test = new TestDML();
int choose = input.nextInt();
switch (choose) {
case 1://新增
test.insertNewGrade();
break;
case 2://删除
test.delGrade();
break;
case 3://修改
test.updateGrade();
break;
case 4://查询
test.selectAll();
break;
}
}
private void selectAll() {
BaseDao bd = new BaseDao();
Connection conn = null;
String sql = "select * from petstore";
PreparedStatement pstmt=null;
ResultSet rs = null;
try {
conn = bd.getConnection();//获取数据库链接对象
pstmt = bd.createPreparedStatement(conn, sql);
rs = pstmt.executeQuery();
System.out.println("id\\tname\\tpassword\\tbalance");
while(rs.next()){
System.out.println(rs.getInt(1)+"\\t"+rs.getString(2)+"\\t"+rs.getString(3)+"\\t"+rs.getInt(4));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
bd.closeAll(conn,pstmt,rs);
}
}
private void updateGrade() {
System.out.println("请输入要修改的id编号:");
int Id = input.nextInt();
System.out.println("请输入新的id:");
int id = input.nextInt();
System.out.println("请输入新的name:");
String name = input.next();
System.out.println("请输入新的password:");
String password = input.next();
System.out.println("请输入新的balance:");
String balance = input.next();
//执行插入语句,需要传入sql和params
BaseDao bd = new BaseDao();
String sql = "update petstore set id=?, name=?,password=?,balance = ? where id=?";
Object[] params = {id,name,password,balance,Id};
int result = bd.executeDML(sql, params);
if(result>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
private void delGrade() {
System.out.println("请输入要删除的id:");
int id = input.nextInt();
//执行插入语句,需要传入sql和params
BaseDao bd = new BaseDao();
String sql = "delete from petstore where id=?";
Object[] params = {id};
int result = bd.executeDML(sql, params);
if(result>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
private void insertNewGrade() {
//执行新增(年级编号,年级名称)
System.out.println("请输入id:");
int id = input.nextInt();
System.out.println("请输入name:");
String name = input.next();
System.out.println("请输入password:");
String password = input.next();
System.out.println("请输入balance:");
String balance = input.next();
//执行插入语句,需要传入sql和params
BaseDao bd = new BaseDao();
String sql = "insert into petstore value (?,?,?,?)";
Object[] params = {id,name,password,balance};
int result = bd.executeDML(sql, params);
if(result>0){
System.out.println("新增成功");
}else{
System.out.println("新增失败");
}
}
}
4、效果,下面是表中原有的一个数据
1、插入
2、删除
3、更新
4、查看表数据
以上!!!!!