IDEA+MySQL的jdbc的增删改查操作

IDEA+MySQL的jdbc的增删改查操作

一、连接数据库

打开IDEA和MySQL,先进行数据库连接,部分代码:
//1加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2创建连接
connection = DriverManager.getConnection(“jdbc:mysql://localhost:3306/数据库名?useSSL=true&characterEncoding=utf-8&user=用户名&password=密码”);
上面的语句可以连接成功数据库。
代码优化: 这里面将数据库的连接以及之后的关闭进行封装

public class DBUtil {
    public static Connection connenction(){
        Connection connection =null;
        try {
            //1加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2创建连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8&user=root&password=123");
        } catch (Exception e) {
            e.printStackTrace();
        }
       return connection;
    }
        public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
            //7关闭资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

二、进行jdbc的增删改查操作

1.查找的SQL语句
// 声明变量
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;

//3.写sql
String sql=“select * from 数据库中的表名”;
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql
resultSet = statement.executeQuery();
2.增删改操作的SQL语句
tring sql1=“insert into student values(‘zhang’,‘13’,‘05’,‘women’)”;//增
String sql2=“delete from student where sname=‘zzy’”;//删
String sql3=“update student set sex=‘girl’ where sname=‘wzq’”;**//改

statement.execute(sql1);
statement.execute(sql2);
statement.execute(sql3);

3.处理结果集
//6处理结果集
while (resultSet.next()){
System.out.print(" sname: "+resultSet…getString(1))
;
//本人的表中有4列,所以下面有4个,要注意是int型还是char型
System.out.print(" age: "+resultSet.getString(2));
System.out.print(“snum: “+resultSet.getString(3));
System.out.print(” sex: “+resultSet.getString(4));
System.out.println(”-----------”);
}

经过上面的操作,可以对数据库中表进行增删改查

三、关闭

关闭资源的代码,如下
finally {
//7关闭资源
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

以上就是jdbc操作的主要操作代码,下面补全上面连接数据库关闭资源之外的其余代码;

package p1;
import bean.student;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class jdbc {
    public List findAll () {
        List list = new ArrayList<>();
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        try {
            connection = DBUtil.connenction();
            //3.写sql
            String sql = "select * from student";//student表
            //4.得到statement对象
            statement = connection.prepareStatement(sql);
            //5.执行sql
            resultSet = statement.executeQuery();
            //6处理结果集
            while (resultSet.next()) {
                String sname = resultSet.getString(1);
                String age = resultSet.getString(2);
                String snum = resultSet.getString(3);
                String sex = resultSet.getString(4);
                student info = new student(sname, age, snum, sex);//定义info 保存每条信息
                list.add(info);
            }
            String sql1="insert into student values('zhang','13','05','women')";//插入
            String sql2="delete from student where sname='zzy'";//删除
            String sql3="update student set sex='girl' where sname='wzq'";//改
                 
            statement.execute(sql1);
            statement.execute(sql2);
            statement.execute(sql3);           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //7关闭资源
            DBUtil.close(connection, statement, resultSet);
        }
        return list;
    }
    public static void main(String[] args) {
        jdbc Jdbc=new jdbc();
        List list = new ArrayList<>();
        list =Jdbc.findAll();
        System.out.println(list.toString());
    }
}

下面的代码块可自动生成,具体的步骤不予以介绍,代码如下:

package bean;
public class student {
    public student(String sname, String age, String snum, String sex) {
        this.sname = sname;
        this.age = age;
        this.snum = snum;
        this.sex = sex;
    }
    private String sname;
    private String age;
    private String snum;
    private String sex;
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSnum() {
        return snum;
    }
    public void setSnum(String snum) {
        this.snum = snum;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "student{" +
                "sname='" + sname + '\'' +
                ", age='" + age + '\'' +
                ", snum='" + snum + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

以上就是本人对jdbc的增删改查最新的学习成果,会随时更新,因为本人也是刚刚开始慢慢学习,有不足之处请见谅啊!
本人已经可以运行代码,且可以对数据库进行各项操作

你可能感兴趣的:(Java,idea)