JAVA通过JDBC操作MySQL数据库(二):Statement接口操作数据库
JAVA项目配置MySQL驱动文件
JDBC是JAVA操作关系型数据库的一个标准,操作数据库的接口由JAVA提供,但是具体的实现不由JAVA负责,而是由各种数据库提供的驱动文件负责,类似于JAVA中的native方法,native方法JAVA只是提供接口不提供具体实现,具体实现有操作系统完成。实现MySQL数据库具体操作的就是MySQL针对JAVA的的驱动文件
驱动文件下载
在MySQL官网 https://www.mysql.com/ 中DOWNLOADS—Community—MySQL Connectors—Connector/J 就是JAVA中使用的MySQL的驱动文件,操作系统选择Platform Independent,Windows系统下载zip文件,如下图所示:
下载下来的文件中的mysql-connector-java-8.0.13.jar就是我们要在JAVA工程中配置的驱动文件。
配置驱动文件
在Eclipse中选中你要使用MySQL的JAVA工程,右键—Properties—Java Build Path—Libraries—Add-Externel JARs,选择mysql-connector-java-8.0.13.jar,这样mysql-connector-java-8.0.13.jar就出现在了本项目的Libraries中,如下图所示,然后选择OK。
连接数据库(Connection接口)
通过JDBC操作数据库的类和接口都在java.sql包中,我们主要使用的是java.sql.Connection、java.sql.Statement、java.sql.PreparedStatement三个接口和DriverManager类。
数据库的连接步骤:
- 加载驱动类,但此项已经不需要,程序会自动加载
- DriverManeger产生Connection的实例化对象(public static Connection getConnection(String url,String user,String password) throws SQLException),Connection的实例化对象不为空表明数据库连接成功。其中url为数据库的地址,地址随数据库类别不同而不同,MySQL的地址模式是 jdbc:mysql://主机名:端口名/具体数据库名 ;主机名和端口号可以在Workbench中看到,在JAVA通过JDBC操作MySQL数据库(一):MySQL数据库准备 中有讲到,具体数据库名就是我们要操作的数据库名。要连接我的电脑上的gc数据库,那么url应该为jdbc:mysql://localhost:3306/gc? serverTimezone=GMT ,其中 ? serverTimezone=GMT 为指明时间标准,因为数据库的操作涉及到时间、数据库的操作历史也需要时间,如果不加 ? serverTimezone=GMT 的话会报错。
- 数据库属于资源操作,操作结束后应该关闭连接。
public class Mydemo {
private static final String DBDRIVER="com.mysql.cj.jdbc.Driver";
//MySQL 8.0的Driver所在包发生了变化,不再是com.mysql.jdbc.driver
private static final String DBURL="jdbc:mysql://localhost:3306/gc? serverTimezone=GMT";
private static final String DBUSER="root";
private static final String DBPASSWORD="root";
public static void main(String[] args) throws Exception {
//1.加载数据库驱动类,但此项现在已经不需要,程序会自动加载
Class.forName(DBDRIVER);
//2.连接数据库,产生Connection的实例化对象
Connection connection=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
System.out.println(connection);//如果输出结果不为空那么数据库连接成功
connection.close();
}
}
操作数据库(Statement接口)
操作数据库的主要内容为:
- Connection产生Statement的实例化对象(Statement createStatement() throws SQLException )。
- 更新数据库(int executeUpdate(String sql) throws SQLException),String sql 是完整的SQL语言命令组成的字符串,返回的int型数据是改语句更新数据库的行数。
- 查询数据库(ResultSet executeQuery(String sql) throws SQLException),ResultSet 中保存所有的查询结果集,根据next() 方法从第一条结果开始往后判断结果集是否有数据,getString(int columnIndex) 或者getString(String columnLable) 返回String类型的数据,类似的还有*getInt()*等。
更新数据库(插入、删除、修改)
以数据库gc中的student表为例,原始表如下:
更新数据库代码:
public class Mydemo {
private static final String DBURL="jdbc:mysql://localhost:3306/gc? serverTimezone=GMT";
private static final String DBUSER="root";
private static final String DBPASSWORD="root";
public static void main(String[] args) throws Exception {
Connection connection=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
Statement st=connection.createStatement();//产生Statement实例化对象
String sql=" insert into student values('zx',20,'chengdu','new student','1998-11-8') ";//插入数据
//完整的SQL语句,sql前后最好有空格,防止在换行的时候出现不可知错误。
String sql1=" delete from student where name='zs' ";//删除数据
String sql2=" update student set city='xiamen' where name='ls' ";//修改数据
int len=st.executeUpdate(sql);//执行更新操作
System.out.println(len);
len=st.executeUpdate(sql1);
System.out.println(len);
len=st.executeUpdate(sql2);
System.out.println(len);
connection.close();
}
}
执行后的结果如下:
查询数据库
以上图中student表中的数据为准,查询代码如下:
public class Mydemo {
private static final String DBURL="jdbc:mysql://localhost:3306/gc? serverTimezone=GMT";
private static final String DBUSER="root";
private static final String DBPASSWORD="root";
public static void main(String[] args) throws Exception {
Connection connection=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
Statement st=connection.createStatement();//产生Statement实例化对象
String sql=" select name,age,city from student ";//查询数据
ResultSet result=st.executeQuery(sql);//执行查询操作
while (result.next()) {
//以列名:columnIndex取出数据
// String name=result.getString("name");
// int age=result.getInt("age");
// String city=result.getString("city");
//以列序号columnLabel取出数据
String name=result.getString(1);
//注意这里的序号是以sql中的查询顺序写的,不是数据库本身中的列顺序,为了方便一般使用序号,因此最好不要使用select *
int age=result.getInt(2);
String city=result.getString(3);
System.out.println(name+"、"+age+"、"+city);
}
connection.close();
}
}
主要注意的是,本文中讲的Statement接口在操作数据库时有其缺点,所以在编程时基本上不使用Statement接口,而是使用PreparedStatement接口,这里之所以将Statement接口是为了更好的过渡到PreparedStatement,PreparedStatement的讲解在JAVA通过JDBC操作MySQL数据库(三):PreparedStatement接口操作数据库
系列博客:
JAVA通过JDBC操作MySQL数据库(一):MySQL数据库准备
JAVA通过JDBC操作MySQL数据库(三):PreparedStatement接口操作数据库