JDBC快速入门(附Java通过jar包连接MySQL数据库)

 

•通过jar包连接mysql数据库


•下载jar包

Java 连接 MySQL 需要驱动包,官网下载地址为MySQL驱动包官网下载,选择适合的jar包版本进行安装

(记得安装的地址,下面导入包时会用到)

•通过build path导入

在想要连接数据库的项目名上右键->Build Path->Configure Build Path

 打开另一个页面后,点击Libraries(箭头所示)

选择Add External JARs…,选择我们要添加的jar包(即上面安装的地址)导入,Apply----Apply and Close即可。


 

•JDBC连接数据库的步骤

1:注册驱动(Driver)

2:创建数据库连接(Connection)

3:   定义SQL语句

4:  创建SQL命令发送器Statement

5:通过Statement发送SQL命令并得到结果

6:处理SQL结果

7:关闭数据库资源

package JDBCtest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo {
    public static void main(String[] args) throws Exception {
        // 1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");//Mysql 5之后这句可以不用写,会自动填充

        // 2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/test";
        String username = "root";
        String password = "1234";
        Connection connection = DriverManager.getConnection(url, username, password);

        // 3.定义sql
        String sql = "select * from student";

        // 4.获取执行sql的对象Statement
        Statement statement = connection.createStatement();

        // 5.执行sql
        ResultSet rs = statement.executeQuery(sql);
        
        //6.处理结果,ResultSet类用来存放获取的结果集
        String sno = null;
        String sname = null;
        int age = 0;
        String cno = null;
        int num = 0;
        System.out.println("sno" + "\t" + "sname" + "\t" + "age" + "\t" + "cno" + "\t" + "num");
        while (rs.next()) {
            sno = rs.getString("sno");
            sname = rs.getString("sname");
            age = rs.getInt("age");
            cno = rs.getString("cno");
            num = rs.getInt("num");
            System.out.println(sno + "\t" + sname + "\t" + age + "\t" + cno + "\t" + num);
        }
        //7.释放资源
        rs.close();
        statement.close();
        connection.close();
    }
}

将所查询的数据保存到ArrayList中(方便上传到网站等一系列的操作)

①测试类

package JDBCtest;

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

public class Demo {

    public static void main(String[] args) throws Exception {

        // 创建List
        ArrayList<Account> list = new ArrayList<Account>();
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 创建连接
        String url = "jdbc:mysql:///test";
        String username = "root";
        String password = "1234";
        Connection connection = DriverManager.getConnection(url, username, password);
        // sql语句
        String sql = "select * from account";
        // 获取执行sql语句的Statement
        Statement statement = connection.createStatement();
        // 执行sql
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            Account ac = new Account();
            ac.setAname(rs.getString("aname"));
            ac.setMoney(rs.getInt("money"));
            list.add(ac);
        }
        // 释放资源
        statement.close();
        connection.close();
        for (Account a : list) {
            a.get();
        }
    }
}

②Account类

package JDBCtest;

public class Account {
    private String aname;
    private int money;

    public void get() {
        // TODO Auto-generated method stub
        System.out.println(aname + " " + money);
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

 

posted @ 2022-03-22 13:05  MMMMinoz  阅读(1224)  评论(1编辑  收藏  举报