JDBC对数据库的基本操作
2009-04-24 21:27
441 查看
第N章 使用数据库
N.1 连接数据库
N.2 执行SELECT语句
N.3 执行DML语句
N.4 参数化SQL
N.5 使用存储过程
N.6 使用事务
N.7 操作大数据字段
N.1 连接数据库
连接数据库时,需要数据库厂商提供的JAVA引擎,这实在是一个比较无奈的事情。连接数据库的语法如下所示:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.Connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.DriverManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Class.forName("类名").newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection con = DriverManager.getConnection("连接字符串", "用户名", "口令");
下表提供了常用数据库的JAVA引擎及连接字符串
数据库 | 驱动提供者 | 驱动类 | 连接字符串 |
---|---|---|---|
SQL Server | Microsoft | com.microsoft.jdbc.sqlserver.SQLServerDriver | jdbc:microsoft:sqlserver://服务器名或IP:端口号(默认为1433); DatabaseName=pubs; SelectMethod=cursor |
jTDS | net.sourceforge.jtds.jdbc.Driver | jdbc:jtds:sqlserver://服务器名或IP:端口号(默认为1433)/数据库名 | |
ORACLE | Oracle | oracle.jdbc.driver.OracleDriver | jdbc:oracle:thin:@服务器名或IP:端口号(默认为1521):数据库SID |
ODBC | Sun | sun.jdbc.odbc.JdbcOdbcDriver | jdbc:odbc:数据源名称 |
import java.sql.Connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.DriverManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection con = DriverManager.getConnection(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs;SelectMethod=cursor",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"sa",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"sa");
N.1.1 SQL Server
如果要使用Windows 身份验证方式连接SQL Server,使用微软的开发包我还没找出来,使用jTDS的话,连接字符串可以这样写:![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection connection =
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
DriverManager.getConnection(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"jdbc:jtds:sqlserver://localhost:1433/pubs;domain=a",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"administrator",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"liulindong");
因为我的电脑不在域中,所在domain我是随便写的,但不能为空,后面两个参数是windows的用户名和口令,呵呵。
N.2 执行SELECT语句
下面是一个简单的例子,它连接本机的SQL SERVER pubs库,输出authors表的全部内容。![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package example;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.Connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.DriverManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.ResultSetMetaData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.ResultSet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.Statement;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Test ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void main(String[] args) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Connection con = DriverManager.getConnection(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs;SelectMethod=cursor",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"sa",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"sa");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Statement st = con.createStatement();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String sql = "select * from authors";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ResultSet rs = st.executeQuery(sql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ResultSetMetaData meta_data = rs.getMetaData();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(meta_data.getColumnLabel(i_col) + " ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(rs.next())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(rs.getString(i_col) + " ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rs.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
st.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
从上例中,我们可获知如下几点内容:
使用java.sql.Statement.executeQuery来执行SELECT语句
它返回一个ResultSet类型,使用next()方法遍历
使用ResultSetMetaData来获取结果集的一些统计信息,如列名、列数、列类型等
使用ResultSetMetaData和ResultSet的get*()类的成员函数时,如果以整数值作为索引,则索引值从1开始。
注意:ResultSet没有提供方法可以获取结果集的记录条数,所以,如果想要获取记录条数,需要使用count()函数自己去查。网上有些人提出的所谓解决方法并不好用,说明如下:
使用ResltSet.last()和ResultSet.getRow()以获取记录条数,但很可惜,有些JDBC的驱动并不实现last()函数,例如JDBC-ODBC、SQL Server,其实这些JDBC驱动不实现除next()外的所有的定位操作,包括:first()、last()、previous()、absolute()等
使用对next()方法进行计数,方法可行,但且不说效率如何,只说我们获取了结果集,并不是只是为了得到一个结果集记录条数的。循环完了,我们怎么回去呢,因为对某些JDBC驱动,可并不提供first()方法哦。
N.3 执行DML语句
使用Statement.executeUpdate()方法或Statement.execute()方法N.4 参数化SQL
使用PreparedStatement对象![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
pstmt.setBigDecimal(1, 153833.00)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
pstmt.setInt(2, 110592)
N.5 使用存储过程
N.5.1 带返回参数的存储过程
针对如下的表:![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table test_table(a int, b int)
有如下存储过程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create procedure [sp_test]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
@a int, @b int, @count int output
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
with recompile
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
as
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set nocount on
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
insert into test_table(a, b) values(@a, @b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select @count = count(*) from test_table
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
Java的示例代码如下所示:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection conn = ...; //获取Connection
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String sql = "exec sp_test ?, ?, ?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CallableStatement st = conn.prepareCall(sql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.setInt(1, 10); //设置输入参数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.setInt(2, 10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.registerOutParameter(3, Types.INTEGER); //设置输出参数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.execute();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.out.println(st.getInt(3)); //获取输出参数的值
N.5.2 返回结果集的存储过程
这个简单,直接使用executeQuery()即可。也不一定用CallableStatement对象,Statement(如果没参数)和PrepareStatement均可。N.6 使用事务
一般使用的步骤如下所示:![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection conn = ...; //获取Connection对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
conn.setAutoCommit(false); //取消自动提交
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
...(数据库操作)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn.commit(); //提交更改
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
catch(Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn.rollback(); //回滚事务
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ex.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
conn.setAutoCommit(true); //恢复自动提交
N.7 操作大数据字段
大数据字段是指ORACLE下的BLOB、CLOB,SQL Server下的image、text等类型的字段。N.7.1 示例表
假设有如下的SQL SERVER表![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table test_table(id int, content image)
N.7.2 引用的类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.File;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.Connection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.DriverManager;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.PreparedStatement;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.ResultSet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.sql.Statement;
N.7.2 插入
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection connection = ... //获取连接
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String sql;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
sql = "insert into test_table(id, content) values(?, ?)";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PreparedStatement st = connection.prepareStatement(sql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.setInt(1, 20);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String file_path = "c:/1.xls"; //要读取的文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//获取文件长度并分配空间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
File f = new File(file_path);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
byte[] data = new byte[(int)f.length()];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//读取文件内容并插入
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FileInputStream fs = new FileInputStream("c:/1.xls");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
fs.read(data);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.setObject(2, data);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
st.executeUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
fs.close();
N.7.3 读取
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection connection = ... //获取连接
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String sql = "select content from test_table where id = 10";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Statement st2 = connection.createStatement();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
ResultSet rs = st2.executeQuery(sql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
rs.next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
InputStream outStream = rs.getBinaryStream(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FileOutputStream fw = new FileOutputStream("c:/2.xls", false); //输出到c:.xls
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int buf_len = 1024;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
byte[] out_buf = new byte[buf_len];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int read_len = outStream.read(out_buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while(read_len > 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fw.write(out_buf, 0, read_len);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
read_len = outStream.read(out_buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
fw.flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
fw.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
outStream.close();
相关文章推荐
- JDBC操作数据库的基本流程
- JAVA数据库JDBC操作基本步骤和防注入
- JDBC操作数据库的基本流程
- JDBC连接数据库和基本操作
- 学习笔记_JDBC_1_Demo1_连接数据库的基本操作和步骤
- java中简述使用JDBC完成数据库操作的基本步骤。
- 原生无框架JDBC实现数据库连接和Servlet页面的基本操作
- jdbc连接数据库的基本操作
- 学习笔记_JDBC_1_Demo1_连接数据库的基本操作和步骤
- JDBC操作数据库基本步骤
- 使用JDBC操作数据库(CRUD基本操作)
- jdbc操作数据库的基本流程详解
- JDBC之数据库基本操作
- 数据库基本操作和JDBC编程步骤
- JDBC操作数据库的基本步骤
- JDBC操作数据库的基本步骤
- 02. JDBC 数据库基本操作
- JDBC对数据库的基本操作
- JDBC操作数据库基本封装
- [Java] JDBC 01 初步 , java 连接 数据库的基本六步操作