IDEA JDBC 链接,增,删,改操作

1.创建数据库

create database mydb

2.创建表 添加数据

create table employ(
	name varchar(16) not null,
	sex varchar(2) not null,
	age int ,
	address varchar(32) 
)


INSERT into employ VALUE
	('张三','男',18,'河南省南阳市'),
	('李杰','女',18,'河南省郑州市'),
	('Rose','女',19,'美国'),
	('Jack','男',21,'英国');

(二)开始连接

1.抛出异常

有两种抛出方式,第一种为直接抛出,这种没有什么异常的话可以直接执行

//使用throws Exception
//如:
public static void main(String[] args) throws Exception

第二种可以处理异常,有异常不会导致java虚拟机关闭

//2.可以使用try{}catch{}
//如
try {
	/*这里放置执行的代码*/
    }catch (){
    /*这里放释放的代码*/
		}  

2.加载驱动

加载驱动的基本语法为

Class.forName("");

8.0版本后的语法为

Class.forName("com.mysql.cj.jdbc.Driver");

3.创建mysql的连接

/**
    * 参数1  url:数据库地址"jdbc:mysql://localhost:3306/mtdb"
	* 		jdbc:协议
	* 		mysql:使用的是mysql数据库
	* 		localhost:数据库主机名 
	* 		因为连接的是本地的数据库 所以可以使用localhost 
	* 		也可以使用IP地址127.0.0.2
	* 		3306:数据库的端口号
	* 		youxi:连接的那个数据库名
	* 
	* 参数2  user: 登录数据库的用户名
	* 参数3  passeord: 登录数据库的密码
	* 
	*/
String url="";
/*连接数据库的账号,user账号的格式,如未经特殊修改可以为root*/
String user="root";

String password="root";
//执行sql对象
Connection conn = DriverManager.getConnection(url, user, password);

mysql查询用户名的语法为

select * from employ;

5.创建操作对象

有三种方式创建操作对象
1)普通执行sql对象

Employ employ = new Employ();

 2)预编译sql的执行sql对象,防止sql注入
当使用该语句时sql语法的关键字需要为"? “如select * from std where id=? ,”?"为占位符

PreparedStatement ps = con.prepareStatement(sql);

6.获取操作返回值

ResultSet rs=ps.executeQuery();

7.回收资源, 先关闭rs结果集对象  再ps预处理对象  最后con连接对象

 if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }

查询源码

自己创建 Employ 类

package JDBC;

public class Employ {
    private String name ;
    private String sex ;
    private int age ;
    private String address ;



    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public int getAge() {
        return age;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public String toString() {
        return "employ{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}
package JDBC;

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

public class JDBCtest01 {
    /**
     * 连接数据库需要配置四大参数,同时需要驱动jar包
     */
    public static void main(String[] args) throws Exception {

        String driver = "com.mysql.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3306/mydb";

        String user = "root";

        String password = "root";


        // 1.导入jar包
        // 2.加载驱动
        Class.forName(driver);
        // 3.通过驱动管理器获得数据库的连接对象
        Connection con = DriverManager.getConnection(url, user, password);
        // 4.使用Connection对象创建sql语句
        String sql = "select * from employ";
        PreparedStatement ps = con.prepareStatement(sql);
        // 5.执行sql语句
        ResultSet rs = ps.executeQuery();
        ArrayList<Employ> employlist = new ArrayList<Employ>();

        // 5.1遍历结果集
        while (rs.next()) {
            Employ employ = new Employ();
            String name = rs.getString("name");
            String sex = rs.getString("sex");
            int age = rs.getInt("age");
            String address = rs.getString("address");

            employ.setName(name);
            employ.setAge(age);
            employ.setAddress(address);
            employ.setSex(sex);
            employlist.add(employ);

        }
        for (Employ employ : employlist) {    //foreach遍历查看集合
            System.out.println(employ.toString());
        }
        // 6.回收资源
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
    }
}

新增

 

package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class JDBCtest02 {
    /**
     * 连接数据库需要配置四大参数,同时需要驱动jar包
     */
    public static void main(String[] args) throws Exception {

        String driver = "com.mysql.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3306/mydb";

        String user = "root";

        String password = "root";


        // 1.导入jar包
        // 2.加载驱动
        Class.forName(driver);
        // 3.通过驱动管理器获得数据库的连接对象
        Connection con = DriverManager.getConnection(url, user, password);
        // 4.使用Connection对象创建sql语句
        String sql = "INSERT into employ(name,sex,age,address) VALUE (?,?,?,?) ";
        PreparedStatement ps = con.prepareStatement(sql);
        // 4.1 赋值
        Employ xm = new Employ();
        xm.setName("小明");
        xm.setSex("男");
        xm.setAge(19);
        xm.setAddress("郑州");

        ps.setObject(1,xm.getName());
        ps.setObject(2,xm.getSex());
        ps.setObject(3,xm.getAge());
        ps.setObject(4,xm.getAddress());
        // 5.执行sql语句
        int a = ps.executeUpdate();

        // 6.处理结果
        if (a > 0) {
            System.out.println("新增成功");
        } else {
            System.out.println("新增失败");
        }
        // 6.1回收资源
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
    }
}

删除

package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class JDBCtest03 {
    /**
     * 连接数据库需要配置四大参数,同时需要驱动jar包
     */
    public static void main(String[] args) throws Exception {

        String driver = "com.mysql.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3306/mydb";

        String user = "root";

        String password = "root";


        // 1.导入jar包
        // 2.加载驱动
        Class.forName(driver);
        // 3.通过驱动管理器获得数据库的连接对象
        Connection con = DriverManager.getConnection(url, user, password);
        // 4.使用Connection对象创建sql语句
        String sql = "delete from employ where name='小明'";
        PreparedStatement ps = con.prepareStatement(sql);
        // 5.执行sql语句
        int a = ps.executeUpdate();

        // 6.处理结果
        if (a > 0) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        // 6.1回收资源
        if (ps != null) {
            ps.close();
        }
        if (con != null) {
            con.close();
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值