博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDBC学习——简单学习JDBC
阅读量:3941 次
发布时间:2019-05-24

本文共 3825 字,大约阅读时间需要 12 分钟。

JDBC学习——简单学习JDBC

1.初始JDBC

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。

简单点来说,JDBC就是Java用来操作数据库的一套接口。

2.JDBC下载和搭建

地址: https://mvnrepository.com/artifact/mysql/mysql-connector-java

点击下载与自己数据库版本相应的jar包

在这里插入图片描述
在这里插入图片描述
然后将下载好的jar包导入到idea中,构建好即可
在这里插入图片描述

3.JDBC步骤

① 加载驱动

② 获取与数据库的链接
③ 获取用于向数据库发送sql语句的statement
④ 向数据库发sql,并获取代表结果集的resultset
⑤ 取出结果集的数据
⑥ 关闭链接,释放资源

public class JDBC1 {    public static void main(String[] args) throws ClassNotFoundException, SQLException {        //1.加载驱动        Class.forName("com.mysql.jdbc.Driver");        //2.获取数据库连接        String username = "root";        String password ="123456";        String url = "jdbc:mysql://localhost:3306/jdbcstudy";        Connection connection = DriverManager.getConnection(url,username,password);        //3.编写SQL语句,获取向数据库发送sql语句的statement对象        String sql = "select id,name,password,email from users";        Statement statement = connection.createStatement();        //4.提交sql语句,返回值为一个结果集:executeQuery(查询)、executeUpdate(增删改)        ResultSet resultSet = statement.executeQuery(sql);        //5.循环打印结果集数据        while (resultSet.next()){            System.out.println(resultSet.getObject("id"));            System.out.println(resultSet.getObject("name"));            System.out.println(resultSet.getObject("password"));            System.out.println(resultSet.getObject("email"));        }        //6.释放资源        resultSet.close();        statement.close();        connection.close();    }}

4.JDBC工具类的编写

首先,新建配置文件:jdbc.properties,然后再文件中添加:

driver=com.mysql.jdbc.Driverusername=rootpassword=123456url=jdbc:mysql://localhost:3306/jdbcstudy

再将一些固定代码写到工具类中:

public class JDBCUtils {    private static String url;    private static String username;    private static String password;    private static String qd;	//使用静态代码块是为了在工具类加载时自动加载静态代码块内的代码,而不用手动操作    static {    	//获取配置恩建中的内容        Properties properties = new Properties();        try {            properties.load(new FileReader("jdbc.txt"));        } catch (IOException e) {            e.printStackTrace();        }        qd = properties.getProperty("qd");        url = properties.getProperty("url");        username = properties.getProperty("username");        password = properties.getProperty("password");        try {            Class.forName(qd);        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    //获取数据库连接    public static Connection getConnection() throws ClassNotFoundException, SQLException {        Connection connection = DriverManager.getConnection(url,username,password);        return connection;    }	//释放资源    public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {        if (connection!=null){            connection.close();        }        if (statement!=null){            statement.close();        }        if (resultSet!=null){            resultSet.close();        }    }    public static void close(Connection connection, Statement statement) throws SQLException {        if (connection!=null){            connection.close();        }        if (statement!=null){            statement.close();        }    }}

然后测试:

public class JDBCDemo {    public static void main(String[] args) throws SQLException, ClassNotFoundException {        Connection connection = JDBCUtils.getConnection();        String sql = "insert into users values(?,?)";        PreparedStatement statement = connection.prepareStatement(sql);        statement.setString(1,"saaaass");        statement.setString(2,"654321");        int i = statement.executeUpdate();        if (i>0){            System.out.println("插入成功");        }else {            System.out.println("失败");        }        JDBCUtils.close(connection,statement);    }}

转载地址:http://laiwi.baihongyu.com/

你可能感兴趣的文章
[手机知道] 用IE7调试 JS报没有权限
查看>>
JS 定义数组
查看>>
PHP解决多线程同时读写一个文件的…
查看>>
PHP一段上传文件的代码
查看>>
猴子排队算法
查看>>
猴子排队算法
查看>>
查询系统负载信息 Linux 命令详解
查看>>
增强 SSH 安全性的 7 条技巧
查看>>
this作用域、javascript面向…
查看>>
提高网页在IE和Firefox上的…
查看>>
提高网页在IE和Firefox上的…
查看>>
php的正则表达式 '/\b\w…
查看>>
ThinkPHP的标签制作及标签调用解析…
查看>>
jQuery.proxy()代理、回调方法
查看>>
php操作memcache的使用测试总结
查看>>
JS创建类和对象
查看>>
完整ASCII字符表(转)
查看>>
jquery事件重复绑定解决办法
查看>>
jQuery.extend 函数详解
查看>>
mysqli_query和mysql_query有何区…
查看>>