IDEA中使用jdbc连接数据库

1.下载连接mysql所需要的jar包,下载之后解压
传送门
IDEA中使用jdbc连接数据库_第1张图片
2.将jar包添加到该项目的库中
IDEA中使用jdbc连接数据库_第2张图片
IDEA中使用jdbc连接数据库_第3张图片
IDEA中使用jdbc连接数据库_第4张图片
3.编写代码

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '用户名',
  `age` int(11) COMMENT '年龄',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

IDEA中使用jdbc连接数据库_第5张图片

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBC {
 
    public static void query() {
        try {
            /**
             将mysql驱动注册到DriverManager中去
             将"com.mysql.jdbc.Driver" 当做参数传入,就是告诉JVM,去"com.mysql.jdbc"这个路径下找Driver类,将其加载到内存中。
             */
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
            String user = "root";
            String password = "xxx";
            //建立和数据库的连接,并返回表示连接的Connection对象
            Connection connection = (Connection) DriverManager.getConnection(url, user, password);
            //要执行SQL语句,必须获得java.sql.Statement实例
            Statement statement = (com.mysql.jdbc.Statement) connection.createStatement();
            String sql = "select * from user";
            //ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些行中数据的访问
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                String name = resultSet.getString("user_name");
                String age = resultSet.getString("age");
                System.out.println(name + " " + age);
            }
            //
            /**
             * 操作完成以后要把所有使用的JDBC对象全都关闭,以释放JDBC资源
             * 先关闭resultSet
             * 再关闭statement
             * 最后关闭连接对象connection
             */
            //关闭结果集
            resultSet.close();
            //关闭执行方法
            statement.close();
            //关闭连接
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void insert() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
            String user = "root";
            String password = "xxx";
            Connection connection = (Connection) DriverManager.getConnection(url, user, password);
            /**
             *
             * 使用Statement需要进行拼写SQl语句,辛苦并且容易出错,使用PreparedStatement可以传入带占位符的SQL语句(使用?进行占位)
             */
            String sql = "insert into user(user_name,age) values (?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //注意:该处索引从1开始,而不是0
            preparedStatement.setString(1, "lisi");
            preparedStatement.setInt(2, 22);
            int result = preparedStatement.executeUpdate();
            System.out.println(result);
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void update() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
            String user = "root";
            String password = "xxx";
            Connection connection = (Connection) DriverManager.getConnection(url, user, password);
            String sql = "update user set user_name=?,age=? where id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "xiaowang");
            preparedStatement.setInt(2, 13);
            preparedStatement.setInt(3, 2);
            int result = preparedStatement.executeUpdate();
            System.out.println(result);
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void delete() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
            String user = "root";
            String password = "xxx";
            Connection connection = (Connection) DriverManager.getConnection(url, user, password);
            String sql = "delete from user where id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 2);
            int result = preparedStatement.executeUpdate();
            System.out.println(result);
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java,jdbc,java,intellij-idea)