首页
归档
友情链接
关于
Search
1
在wsl2中安装archlinux
105 阅读
2
nvim番外之将配置的插件管理器更新为lazy
78 阅读
3
2018总结与2019规划
62 阅读
4
PDF标准详解(五)——图形状态
40 阅读
5
为 MariaDB 配置远程访问权限
33 阅读
软件与环境配置
博客搭建
从0开始配置vim
Vim 从嫌弃到依赖
archlinux
Emacs
MySQL
Git与Github
AndroidStudio
cmake
读书笔记
编程
PDF 标准
从0自制解释器
qt
C/C++语言
Windows 编程
Python
Java
算法与数据结构
PE结构
Thinking
FIRE
菜谱
登录
Search
标签搜索
c++
c
学习笔记
windows
文本操作术
编辑器
NeoVim
Vim
win32
VimScript
emacs
linux
文本编辑器
Java
elisp
反汇编
OLEDB
数据库编程
数据结构
内核编程
Masimaro
累计撰写
314
篇文章
累计收到
31
条评论
首页
栏目
软件与环境配置
博客搭建
从0开始配置vim
Vim 从嫌弃到依赖
archlinux
Emacs
MySQL
Git与Github
AndroidStudio
cmake
读书笔记
编程
PDF 标准
从0自制解释器
qt
C/C++语言
Windows 编程
Python
Java
算法与数据结构
PE结构
Thinking
FIRE
菜谱
页面
归档
友情链接
关于
搜索到
1
篇与
的结果
2019-08-25
Java数据库操作
数据库操作是程序设计中十分重要的一个部分,Java内置JDBC来操作数据库JDBC使用JDBC——Java Database connecting Java数据库连接;本质上JDBC定义了操作数据库的一套接口,作为应用程序的开发人员来说只需要创建接口对应的对象即可,而接口的实现由各个数据库厂商去完成。要在应用程序中使用JDBC,需要根据数据库的不同导入对应的jar包。使用步骤如下:导入相应jar包注册驱动获取数据库连接对象定义sql语句获取执行sql语句的对象执行sql并获取结果集对象从结果集中获取数据释放资源相关对象的描述DriverManager在使用JDBC之前需要先注册驱动,也就是告诉JDBC,我们需要导入哪个jar包,这个工作由DriverManager对象来实现,可以调用它里面的方法 registerDriver 来实现,该方法的定义如下:static void registerDriver(Driver driver);这个方法需要传入一个driver 对象,driver对象是具体的数据库厂商来实现,后续相关操作其实是根据这个driver对象来调用相关代码,实现同一套接口操作不同数据库我们查阅相关实现类的代码如下:public class Driver extends NonRegisteringDriver implements java.sql.Driver { // // Register ourselves with the DriverManager // static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } } /** * Construct a new driver and register it with DriverManager * * @throws SQLException * if a database error occurs. */ public Driver() throws SQLException { // Required for Class.forName().newInstance() } }在Driver对象中发现,它在静态代码块中执行了registerDriver方法,也就是说我们只要加载对应的类,类就会自动帮助我们进行注册的操作。所以在第一步注册驱动的代码中可以这样写:Class.forName("org.mariadb.jdbc.Driver"); //加载对应的Driver类到内存中Connection对象注册了驱动之后就是获取数据库的连接对象,在DriverManager中使用getConnection方法获取,它的定义如下:static Connection getConnection(String url); static Connection getConnection(String url, Properties info); static Connection getConnection(String url, String user, String password);上述3个方法中,常用的是第3个,参数分别为: 连接字串、用户名、密码连接字串的格式为: jdbc:数据库类型://数据库IP:端口/数据库名称,比如 jdbc:mariadb://localhost:3306/test获取连接字串的代码如下:Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/study", "root", "root");执行sql语句获取连接对象之后,需要向数据库传递sql语句并执行它,执行sql语句需要使用对象 Statement, 常用的方法如下:boolean execute(String sql); ResultSet executeQuery(String sql); int executeUpdate(String sql);一般可以使用execute来执行相关操作,如果是查询语句,可以使用executeQuery来执行并获取返回的结果集,如果需要执行DELTE、UPDATE、INSERT等语句可以使用executeUpdate来更新数据库我们可以通过 Connection对象的createStatement方法获取一个Statement对象,代码如下:Statement statement = conn.createStatement(); String strSql = "INSERT INTO student VALUES(2, '2b', 28, 78.9, '2017-12-30', NULL)"; statement.execute(strSql); statement.close(); //最后别忘了关闭对象获取返回结果如果我们执行了像insert、delete、update等等语句,可能不需要关注具体的返回结果,但是如果使用的是select语句,则需要获取返回的结果获取select语句返回的结果可以使用 executeQuery 方法,该方法会返回一个结果集对象可以将结果集对象想象成一个二维的数组,保存了查询到的相关数据,每一行代表一条数据,行中的每一列是一个字段的数据。结果集中使用游标来遍历每一行数据。使用get相关函数来获取对应索引的数据。一行遍历完了使用next移动到下一行;其中get相关方法主要有:Blob getBlob(int columnIndex); Blob getBlob(String columnLabel); boolean getBoolean(int columnIndex); boolean getBoolean(String columnLabel); byte getByte(int columnIndex); byte getByte(String columnLabel); byte[] getBytes(int columnIndex); byte[] getBytes(String columnLabel); Date getDate(int columnIndex); Date getDate(int columnIndex, Calendar cal); Date getDate(String columnLabel); Date getDate(String columnLabel, Calendar cal); double getDouble(int columnIndex); double getDouble(String columnLabel); float getFloat(int columnIndex); float getFloat(String columnLabel); int getInt(int columnIndex); int getInt(String columnLabel); long getLong(int columnIndex); long getLong(String columnLabel);在获取了结果之后需要关闭对应对象清理资源,这部分只需要调用对应的cloase方法即可最终一个完整的demo 如下:public class JDBCDemo1 { public static void main(String[] args) { Connection conn = null; Statement statement = null; ResultSet resultSet = null; try { Class.forName("org.mariadb.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/test", "root", "root"); String sql = "select * from student"; statement = conn.createStatement(); resultSet = statement.executeQuery(sql); while (resultSet.next()){ int id = resultSet.getInt(1); //注意:这里面的索引是从1开始的 String name = resultSet.getString(2); int age = resultSet.getInt(3); double score = resultSet.getDouble(4); Date birthday = resultSet.getDate(5); Timestamp insertTime = resultSet.getTimestamp(6); System.out.println(id + "\t" + name + "\t" + age + "\t" + score + "\t" + birthday + "\t" + insertTime); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try{ if (resultSet != null){ resultSet.close(); } if (statement != null){ statement.close(); } if (conn != null){ conn.close(); } }catch (SQLException e){ e.printStackTrace(); } } } }参数化查询我们知道使用sql拼接的方式来执行sql语句容易造成sql注入漏洞,即使针对某些关键字进行过滤也很难消除这个漏洞,一旦存在sql注入,那么数据库中的数据很容易就会被黑客窃取。而使用参数化查询的方式可以从根本上消除这个漏洞。jdbc中参数化查询使用的对象是 PreparedStatement, 它与Statement对象不同在于,它会提前将sql语句进行编译,后续只会接收固定类型的参数;而Statement只会简单的去执行用户输入的sql语句。在进行参数化查询的时候需要先准备sql语句,但是在查询参数中需要使用 ? 做标记,表示这个位置是一个参数,后续在真正执行前再传入,比如说可以准备这样的sql语句 update student set score = 100 where name = ?。准备好sql语句之后,需要设置对应参数位置的值,我们可以使用 setXxx 方法来设置,setXxx 方法与之前介绍的get方法类似,根据不同的数据类型 Xxx 有不同的取值。设置完参数之后,与Statement 一样,调用对应的execute方法来执行即可.String sql = "update student set score = 100 where name = ?"; ps = conn.prepareStatement(sql); ps.setString(1, "2b"); ps.executeUpdate();数据库连接池在需要频繁操作数据库的应用中,使用数据库连接池技术可以对数据库操作进行一定程度的优化。原理请自行百度。如果要自己实现数据库连接池需要实现 javax.sql.DataSource 的getConnection方法。当然我学习Java只是为了学习一下Web相关的内容,并不想太过于深入,所以自然不会去管怎么实现的,只要调用第三方实现,然后使用就好了。常见的开源的第三方库有: Apache commons-dbcp、C3P0 、Apache Tomcat内置的连接池(apache dbcp)、druid(由阿里巴巴提供)。本着支持国产的心态,这次使用的主要是 druid。druid 连接池需要提供一个配置文件来保存数据库的相关内容driverClassName=org.mariadb.jdbc.Driver url=jdbc:mariadb://localhost:3306/study username=root password=masimaro_1992 # 初始化时连接池中保留连接数 initialSize=5 # 最大连接数 maxActive=10 # 最大时间,超过这个时间没有任何操作则会关闭连接 maxWait=3000在使用时主要需要如下步骤:加载配置文件调用 DruidDataSourceFactory.createDataSource 方法传入 配置,获取到 DataSource 对象调用DataSource.getConnection 方法获取Connection 对象执行后续操作相关代码如下:Connection conn = null; Statement statement = null; Properties properties = new Properties(); try { properties.load(JDBCDemo3.class.getResourceAsStream("druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); conn = dataSource.getConnection(); statement = conn.createStatement(); //do something } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
2019年08月25日
6 阅读
0 评论
0 点赞