Java应用程序连接数据库--JDBC基础
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java应用程序连接数据库--JDBC基础,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含14846字,纯文字阅读大概需要22分钟。
内容图文
![Java应用程序连接数据库--JDBC基础](/upload/InfoBanner/zyjiaocheng/478/d216b2118ae44ca988bbe340c6f9ab17.jpg)
<!-- MySQL驱动,连接数据库用,由数据库厂商提供 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
有这个包就行了。
小例子:
import java.sql.*;
import java.util.Properties;
public class testConnectJdbc {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
//连接数据库的URL
String url = "jdbc:mysql://ip:3306/表空间";
String user = "username"; //数据库用户名
String password = "password"; //密码
//创建一个驱动程序的类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本,推荐使用
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本,使用该类连接也是可行的
//设置用户名和密码
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
//连接数据库
Connection connection = driver.connect(url,properties);
Statement createStatement = connection.createStatement();
String sql = "SELECT * from t_inv_user where TELE_PHONE = ‘180112043115‘";
ResultSet rs = createStatement.executeQuery(sql);
while(rs.next()){
String id = rs.getString(1);
String name = rs.getString(2);
String gender = rs.getString(3);
System.out.println("id:"+id+" 姓名:"+name+" 性别:"+gender);
}
}
}
-
-
- Java应用程序连接数据库JDBC基础
- 概述
- JDBC模式
- JDBC定义
- 第一个JDBC程序
- JDBC核心API
- Connection接口使用
- 改进
- 执行数据查询操作
- preparedStatement接口使用
- CallableStatement接口使用
- Connection接口使用
- 总结
- 参考资料
- Java应用程序连接数据库JDBC基础
-
概述
- 在学习JDBC之前,操作数据库的步骤:
- 登陆到数据库服务器(不管是使用客户端服务器还是使用命令行),比如MySQL应该执行:mysql -u root -p
- 编写SQL语句
- 发送SQL语句到数据库服务器执行
那么JDBC究竟是什么呢?使用Java程序来操作数据库,后者更加直接的话就是使用Java程序来发送SQL语句的技术称之为:JDBC。
使用JDBC发送SQL的前提:
* 登陆数据库的服务器(连接数据库的服务器)。那么必须要知道哪些条件才能连接数据库的服务器呢(也就是说使用JDBC同样和使用客户端一样需要下面的参数)?
* 数据库服务器的IP地址
* 数据库服务器的端口地址
* 数据库服务器的用户名(通常是root)
* 数据库服务器的密码(自己设置好的)
* 编写SQL语句
* 发送SQL语句到数据库服务器执行
没有JDBC之前,使用驱动程序在Java程序与MySQL、Oracle、SQLServer等数据库之间建立连接,驱动程序就是在该课程中将要学到的内容。那么就存在一个问题:在同一个Java程序与数据库之间建立连接时,驱动程序是否是共用的呢?如果一样,很显然就能节省代码量,但是实际上是不一样的。一定要注意是不一样的。那么就存在一个问题:当底层数据库发生改变之后,数据库的驱动程序也会随之发生变化,那么该怎么解决该问题呢?于是JDBC出现了,目的是为了减轻开发人员的工作量,以提高代码的复用。
JDBC模式
在没有JDBC之前是程序开发人员主动去连接数据库,所以我们需要为各种不同的数据库写驱动程序,但是JDBC出现之后,是数据库来了解我的Java代码,JDBC定好了一套规范(协议),如果数据库要连接我的Java程序,那么需要自己写一个驱动程序,但是你所写的驱动必须遵守我定的规范(协议)。开发人员只需要维护自己的Java程序以及规范(协议,其中协议或者规范是固定的),数据库厂商需要维护自己的数据库以及数据库的驱动程序(驱动程序用来实现Java和数据库的连接)。这样做的优点是:
* java开发人员值需要维护Java应用和一套规范
* 数据库厂商提供具体的Java驱动程序,数据库厂商的底层改变,那么必须相应的提供一套驱动,但是规范并不会发生变化。
现在的关注点是:规范(协议)究竟是什么呢?JDBC
JDBC定义
sun公司设计的一套通用的用Java语言操作不同数据库的接口,在接口中存在大量抽象的方法,这样做减轻了Java应用程序的开发周期以及简介性。JDBC的接口在哪里?在JDK中java.sql.*,和javax.sql.*(在JDK 2.0时出现)。
第一个JDBC程序
package com.juzhutech.jdbctest;
import java.sql.*;
import java.util.Properties;
public class JdbcTest {
public static void main(String[] args) throws SQLException {
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/tpch";
String user = "root"; //数据库用户名
String password = "560128"; //密码
//创建一个驱动程序的类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本,推荐使用
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本,使用该类连接也是可行的
//设置用户名和密码
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
//连接数据库
Connection connection = driver.connect(url,properties);
System.out.println(connection);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
第二种连接数据库的方式:
package com.juzhutech.jdbctest;
import java.sql.*;
import java.util.Properties;
public class JdbcTest {
public static void main(String[] args) throws SQLException {
//第一种连接数据库的办法
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/tpch";
String user = "root"; //数据库用户名
String password = "560128"; //密码
//创建一个驱动程序的类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本,推荐使用
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本,使用该类连接也是可行的
//设置用户名和密码
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
//连接数据库
Connection connection = driver.connect(url,properties);
System.out.println(connection);
test();
}
//第二种连接数据库的方法,使用的是DriverManager
public static void test() throws SQLException{
Driver driver = new com.mysql.jdbc.Driver();
//注册驱动程序,可以注册多个启动程序
DriverManager.registerDriver(driver);
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/tpch";
String user = "root"; //数据库用户名
String password = "560128"; //密码
//连接到具体的数据库
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
在我们实际编程的过程中,以上的两种方法都显得十分的繁琐,实际上看com.mysql.jdbc.Driver就会知道,在该类中有一个静态代码块,只要想办法加载该类就会执行静态代码块中的内容,看下面的实现,这种实现也是平时我们最频繁使用的JDBC连接数据库的方法。
package com.juzhutech.jdbctest;
import java.sql.*;
import java.util.Properties;
public class JdbcTest {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//第一种连接数据库的办法
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/tpch";
String user = "root"; //数据库用户名
String password = "560128"; //密码
//创建一个驱动程序的类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本,推荐使用
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本,使用该类连接也是可行的
//设置用户名和密码
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
//连接数据库
Connection connection = driver.connect(url,properties);
//System.out.println(connection);
test();
}
//第二种连接数据库的方法,使用的是drivermanager
public static void test() throws SQLException, ClassNotFoundException{
//Driver driver = new com.mysql.jdbc.Driver();
//注册驱动程序,可以注册多个启动程序
//DriverManager.registerDriver(driver);
//通过得到字节码对象的方式加载静态大妈快,从而注册驱动程序
Class.forName("com.mysql.jdbc.Driver"); //使用驱动管理器的方式去连接数据库
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/tpch"; //连接不同的数据库体现在url上
String user = "root"; //数据库用户名
String password = "560128"; //密码
//连接到具体的数据库
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
JDBC核心API
全部的API都在java.sql.*和javax.sql.*,下面展示其中核心的API,也是在JDBC编程郭晨中。
- Driver接口:表示Java驱动程序接口,所有具体的数据库厂商都要来实现此接口。
- connect(url,properties):连接数据库的方法
- url:连接数据库的url(用来定位具体的数据库资源)
- url语法:jdbc:mysql(oracle、sqlserver等)://数据库所在主机的IP地址/端口号/具体要使用的数据库名称
- user:数据库的用户名
- password:数据库的用户密码。
- url:连接数据库的url(用来定位具体的数据库资源)
- connect(url,properties):连接数据库的方法
-
DriverManager类:驱动管理器类,用于管理所有注册的驱动程序。
- registerDriver(driver):注册驱动类对象
- Connection getConnection(url,uer,password):获取连接的对象,其返回值为Connection对象
-
Connection接口:表示Java程序和数据库的连接对象,拿到此接口,就表示和数据库已经建立连接
- createStatement:创建statement对象
- preparedStatement():创建PreparedStatement对象
- CallableStatement prepareCall():创建一个CallableStatement对象
-
Statement接口:用于执行静态SQL语句并返回其结果
- executeUpdate(String sql):执行静态的更新SQL语句(DDL、DML)
- executeQuery(String sql):执行静态的查询SQL语句
-
PreparedStatement接口:
- int executeUpdate():执行预编译的更新SQL语句(DDL、DML)
- ResultSet executeQuery():执行预编译的查询SQL语句(DQL)
-
CallableStatement接口:用于执行存储过程的SQL语句(call xx语句的执行)
- ResultSet executeQuery():执行预编译的查询SQL语句(DQL)
-
ResultSet接口:用于封装查询出来的数据,表示结果集
- next():将光标移动到下一行
- getxx():获取列的值
Connection接口使用
- 使用Statement执行静态SQL语句
Statement对象在整个JDBC的使用过程中都充当的是一艘货船的作用,也就是说java应用程序的SQL语句需要使用Statement对象的方法将其运送至数据库服务器端用来执行,与此同时Statement对象也会装载其运行结果,将结果从数据库服务器运行至java应用程序,下面看一个简单的示例:
//创建数据库中表的代码
package com.juzhutech.jdbctest;
import java.sql.*;
import java.util.Properties;
public class JdbcTest {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//第一种连接数据库的办法
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/tpch";
String user = "root"; //数据库用户名
String password = "560128"; //密码
//创建一个驱动程序的类对象
Driver driver = new com.mysql.jdbc.Driver(); //新版本,推荐使用
//Driver driver = new org.gjt.mm.mysql.Driver(); //旧版本,使用该类连接也是可行的
//设置用户名和密码
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
//连接数据库
Connection connection = driver.connect(url,properties);
//System.out.println(connection);
test();
}
//第二种连接数据库的方法,使用的是drivermanager
public static void test() throws SQLException, ClassNotFoundException{
//Driver driver = new com.mysql.jdbc.Driver();
//注册驱动程序,可以注册多个启动程序
//DriverManager.registerDriver(driver);
//通过得到字节码对象的方式加载静态大妈快,从而注册驱动程序
//连接到具体的数据库
Connection connection = null;
//创建一个select * from customer;该怎么做呢?可以把Statement对象看成一艘装在SQL语句的船,可以实现将SQL语句从Java程序
//运送到MySQL数据库服务器上,并且将生成的结果再带回到Java的Statement对象中
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //使用驱动管理器的方式去连接数据库
//连接数据库的URL
String url = "jdbc:mysql://192.168.101.44:3306/amon"; //连接不同的数据库体现在url上
String user = "root"; //数据库用户名
String password = "560128"; //密码
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
//准备SQL语句
String sql = "create table student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20))";
//发送SQL语句到数据库服务器中,用于执行
int count = statement.executeUpdate(sql);
//输出
System.out.println("影响了"+count+"行");
} catch (Exception e) {
throw new RuntimeException(e);
}
//关闭java程序与数据库的连接,关闭的顺序是后打开的先关闭
statement.close();
connection.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
//实现数据库的增、删、改、查功能
package com.jpzhutech.jdbcdml;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 功能:使用Statement执行DML语句,操作的数据库对象为MySQL
* @author 朱君鹏
* @version 1.0
* @category JDBC使用
* */
public class TestJdbcDML {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.101.44/amon";
String user = "root";
String password = "560128";
Connection connection = DriverManager.getConnection(url, user, password); //导包是一定要注意,导入接口的包,不要导入实现类的包
//准备一个SQL语句
String sql = "INSERT INTO student(NAME) VALUES(‘张三‘)"; //注意在VALUES时要使用单引号,使用双引号会发生错误
//创建一个Statement对象
Statement statement = connection.createStatement();
int count = statement.executeUpdate(sql);
statement.close();
connection.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
注意:数据库的增、删、改功能都和上面的代码(插入代码)基本是一致的,唯一不一致的就是sql语句上的区别,值需要将固定的SQL语句写好,放在相应的位置即可实现全部的功能。
改进
从上面的代码中可以看出:在使用JDBC协议连接数据库时必须要做的操作是:Java应用程序与数据库建立连接、关闭连接,这两个步骤是所有的JDBC程序都要做的,所以下面我将这个公共的部分做成一个工具库,看下面的代码:
package com.jpzhutech.jdbcutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
//数据库信息
private static String url = "jdbc:mysql://192.168.101.44/amon";
private static String user = "root";
private static String password = "560128";
static{
try {
Class.forName("com.mysql.jdbc>driver");
} catch (ClassNotFoundException e) {
System.out.println("注册驱动程序失败!");
throw new RuntimeException(e);
}
}
//获取数据库连接的方法,该方法在JDBC操作中是普遍存在的,因此将其包装为一个工具类
public static Connection getConnection(){
try {
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void close(Connection connection,Statement statement) throws SQLException{
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32 <l 本文系统来源:http://www.cnblogs.com/wulinzi/p/6655894.html
内容总结
以上是互联网集市为您收集整理的Java应用程序连接数据库--JDBC基础全部内容,希望文章能够帮你解决Java应用程序连接数据库--JDBC基础所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。