在IDEA中进行jdbc连接数据库,进行增删查改

IDEA中进行jdbc操作,进行增删改查

    • 1.mysql数据库的基础配置
    • 2.IDEA中对数据库的连接
    • 3.写一个JDBC的步骤创建Test类,如下:
    • 4.加载jar驱动
    • 4.利用JDBC来连接数据库
    • 5.利用JDBC来实现增删改查

DAY2

1.mysql数据库的基础配置

1.打开mysql,建立新连接,如图:
在IDEA中进行jdbc连接数据库,进行增删查改_第1张图片
测试成功之后的显示如下:
在IDEA中进行jdbc连接数据库,进行增删查改_第2张图片

2.IDEA中对数据库的连接

1、配置
在窗口的右边有个Database按钮,点击。
在IDEA中进行jdbc连接数据库,进行增删查改_第3张图片
2.如果没有,请点击上方的View(视图)-Tool Windows(工具窗口)-Database。
在IDEA中进行jdbc连接数据库,进行增删查改_第4张图片
3.配置数据库
点击绿色加号-Data Source,选择你需要的连接的数据库
在IDEA中进行jdbc连接数据库,进行增删查改_第5张图片
(这里我拿oracle数据库演示)。填写完配置,点击driver旁边的Oracle,
再点击download,可以自动下载需要的数据库驱动jar包,十分的方便。其他数据库的配置也都大同小异

在IDEA中进行jdbc连接数据库,进行增删查改_第6张图片

4.操作数据库
点击sql的图标右键选择Open Console可以打开控制台,做查询操作还能输出查询时间,很实用
在IDEA中进行jdbc连接数据库,进行增删查改_第7张图片

3.写一个JDBC的步骤创建Test类,如下:

1、把驱动拷贝进入项目下的lib文件夹夹
2、加载数据库驱动
class.forName(‘com.mysql.jdbc.Driver’);
3、建立数据库连接
+ url的格式
jdbc:mysql://localhost:3306/people_manage
协议:子协议://目标IP地址:端口/数据库
+ 建立连接语句
Connection connection = DriverManager.getConnection(
“jdbc:mysql://localhost:3306/people_manage”, “root”,“123456”);
4、执行查询语句
5、释放资源,断开连接

4.加载jar驱动

1.打开Intellij Idea,创建Project,File->New->Project,如图所示:
在IDEA中进行jdbc连接数据库,进行增删查改_第8张图片
2.指定JDK安装目录,再点击Next.
在IDEA中进行jdbc连接数据库,进行增删查改_第9张图片
3.再次点击Next,来到如图所示地方,给项目取名,比如项目命名为:JDBCDemo,最后点击Finish。
在IDEA中进行jdbc连接数据库,进行增删查改_第10张图片
4.至此,项目已成功创建完毕。src->New->Package,添加包名:com.dgd.test。
在IDEA中进行jdbc连接数据库,进行增删查改_第11张图片

4.利用JDBC来连接数据库

1.创建一个目录,用来放mysql的驱动包,类似eclipse的lib目录:JDBCDemo->New->Directory。
在IDEA中进行jdbc连接数据库,进行增删查改_第12张图片
2.把mysql的jar包复制过来。
在IDEA中进行jdbc连接数据库,进行增删查改_第13张图片
3.把jar包添加到项目里,类似eclipse的add to build path功能,快捷键:Ctrl+Alt+Shift+S,弹出图示菜单
在IDEA中进行jdbc连接数据库,进行增删查改_第14张图片
4.选择第一项:JARs or directories。
在IDEA中进行jdbc连接数据库,进行增删查改_第15张图片
5.找到刚才添加的jar包,点击确定。
在IDEA中进行jdbc连接数据库,进行增删查改_第16张图片
6.点击Apply,OK。
在IDEA中进行jdbc连接数据库,进行增删查改_第17张图片
至此,IDEA中关于jdbc的操作也就完成了。

5.利用JDBC来实现增删改查

可以成功运行,读者可以拿去当作例子来运行,测试是否连接成功。
增操作代码

package com.dgd.test;
import java.sql.*;

public class DBTest_add {
    public static void main(String[] args) throws Exception {
        Connection conn = null;
        // 加载驱动类
        Class.forName("com.mysql.jdbc.Driver");
        long start =System.currentTimeMillis();

        // 建立连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo",
                "root", "132456");
        long end = System.currentTimeMillis();
        System.out.println(conn);
        System.out.println("建立连接耗时: " + (end - start) + "ms 毫秒");
        System.out.println("增操作");

        String sql = "insert into demo.demo(name)"
                +"values ('kitty')";
        // 创建Statement对象
        Statement stmt = conn.createStatement();

        // 执行SQL语句,查
        stmt.executeUpdate(sql);
        System.out.println("成功插入新数据"+"kitty");
        /*ResultSet rs = stmt.executeQuery("select * from demo");
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }

        rs.close();*/
        stmt.close();
        conn.close();
    }

}

删操作代码

package com.dgd.test;
import java.sql.*;
public class DBTest_delete {
    public static void main(String[] args) throws Exception {
        Connection conn = null;
        // 加载驱动类
        Class.forName("com.mysql.jdbc.Driver");
        long start =System.currentTimeMillis();

        // 建立连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo",
                "root", "132456");
        long end = System.currentTimeMillis();
        System.out.println(conn);
        System.out.println("建立连接耗时: " + (end - start) + "ms 毫秒");
        System.out.println("删操作");
        // 创建Statement对象
        Statement stmt = conn.createStatement();
        String sql="delete from demo where name='kitty'";
        //sql="delete from 商品销量 where 商品销量.商店编号='"+shop_number+"' and 商品销量.商品编号='"+goods_number+"'";
        // 执行SQL语句,查
        stmt.executeUpdate(sql);
        System.out.println("成功删除leoleo");
        stmt.close();
        conn.close();
    }


}

改操作代码

package com.dgd.test;
import java.sql.*;
public class DBTest_change {
    public static void main(String[] args) throws Exception {
        Connection conn = null;
        // 加载驱动类
        Class.forName("com.mysql.jdbc.Driver");
        long start =System.currentTimeMillis();

        // 建立连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo",
                "root", "132456");
        long end = System.currentTimeMillis();
        System.out.println(conn);
        System.out.println("建立连接耗时: " + (end - start) + "ms 毫秒");
        System.out.println("改操作");
        // 创建Statement对象
        Statement stmt = conn.createStatement();
        String sql="update demo set name='leoleo' where name='leo' ";//生成一条mysql语句
        //sql="update 职工 set 职工姓名='"+worker_name+"',职工性别='"+worker_sex+"' where 职工编号='"+worker_number+"' ";//生成一条mysql语句
        // 执行SQL语句,查
        stmt.executeUpdate(sql);
        System.out.println("成功修改数据leo为leoleo");

        stmt.close();
        conn.close();
    }
}

查操作代码

package com.dgd.test;
import java.sql.*;

public class DBTest_search {
    public static void main(String[] args) throws Exception {
            Connection conn = null;
            // 加载驱动类
            Class.forName("com.mysql.jdbc.Driver");
            long start =System.currentTimeMillis();

            // 建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo",
                    "root", "132456");
            long end = System.currentTimeMillis();
            System.out.println(conn);
            System.out.println("建立连接耗时: " + (end - start) + "ms 毫秒");
            System.out.println("查操作");
            // 创建Statement对象
            Statement stmt = conn.createStatement();

            // 执行SQL语句,查
            ResultSet rs = stmt.executeQuery("select * from demo");
            System.out.println("姓名");
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }

            rs.close();
            stmt.close();
            conn.close();
    }

}

你可能感兴趣的:(在IDEA中进行jdbc连接数据库,进行增删查改)