java连接mysql数据库(JDBC)

java连接mysql数据库(JDBC)

一、使用步骤

1.连接数据库

public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        String url = "jdbc:mysql://localhost:3306/Flower";
        String name = "root";
        String pwd = "root";
        Connection connection = DriverManager.getConnection(url,name,pwd);
        //获取statement对象
        /*Statement statement = connection.createStatement();*/
        String sql = "select * from flower";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //执行语句
        /*ResultSet resultSet = statement.executeQuery(sql);*/
        ResultSet resultSet = preparedStatement.executeQuery(sql);

2.读取数据

while (resultSet.next()){
            System.out.print("[");
            System.out.print( resultSet.getInt("id")+"\t");
            System.out.print( resultSet.getString("name")+"\t");
            System.out.print( resultSet.getInt("price")+"\t");
            System.out.print( resultSet.getString("production"));
            System.out.println("]");
        }

总结

Java连接数据库的步骤
1.加载数据库驱动
(1)先将数据库驱动添加到项目文件中
(2)加载数据库驱动

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

2.创建数据库连接

String url = "jdbc:mysql://localhost:3306/Flower";
String name = "root";
String pwd = "root";
Connection connection = DriverManager.getConnection(url,name,pwd);

3.创建数据库操作语句执行对象
statement对象和preparedstatement对象都可以,preparedstatement是statement的子类,都返回result对象集合
preparedstatement类提供了get和set方法,可以通过生成的result对象集合对插入语句的数据值进行赋值或者打印查询结果

//获取statement对象
        /*Statement statement = connection.createStatement();*/
        String sql = "select * from flower";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //执行语句
        /*ResultSet resultSet = statement.executeQuery(sql);*/
        ResultSet resultSet = preparedStatement.executeQuery(sql);

4.关闭资源
先打开的后关闭

     if (resultSet != null){
            resultSet.close();
        }
        if(preparedStatement != null){
            preparedStatement.close();
        }
        if(connection != null)
        {
            connection.close();
        }

所有代码展示

package com.company.jdbc;

import javafx.scene.control.SplitPane;

import java.awt.*;
import java.sql.*;

public class test1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接
        String url = "jdbc:mysql://localhost:3306/Flower";
        String name = "root";
        String pwd = "root";
        Connection connection = DriverManager.getConnection(url,name,pwd);
        //获取statement对象
        /*Statement statement = connection.createStatement();*/
        String sql = "select * from flower";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //执行语句
        /*ResultSet resultSet = statement.executeQuery(sql);*/
        ResultSet resultSet = preparedStatement.executeQuery(sql);
        //返回地数据存储在result中
        while (resultSet.next()){
            System.out.print("[");
            System.out.print( resultSet.getInt("id")+"\t");
            System.out.print( resultSet.getString("name")+"\t");
            System.out.print( resultSet.getInt("price")+"\t");
            System.out.print( resultSet.getString("production"));
            System.out.println("]");
        }
        if (resultSet != null){
            resultSet.close();
        }
        if(preparedStatement != null){
            preparedStatement.close();
        }
        if(connection != null)
        {
            connection.close();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值