JDBC, Java Database Connecive, Java 数据库连接,是一组专门负责连接并操作数据库的标准,在整个JDBC 中实际上大量的提供的是接口。针对于各个不同的数据库生产商 ,只要想使用JAVA 进行数据库的开发,则对这些标准有所支持。
JDBC 在使用中常见的有以下三类:
JDBC-ODBC 桥连接:是SUN 在JDK的开发包中提供的最标准的一套JDBC 操作类库,使用的时候将JDBC-ODB-数据库,中间要经过一个ODBC 的连接,那么就意味着整体的性能将会降低,所以在开发中是绝对不会去使用JDBC-ODBC的连接方式的。
JDBC 连接,使用各个数据库提供商给定的数据库驱动程序,完成JDBC的开发,使用的时候需要在classpath中配置数据库的驱动程序
JDBC 网络连接:主要使用通过网络连接数据库
JDBC 的操作步骤
在进行JDBC 操作的时候可以按照以下的步骤完成:
1、加载数据库驱动程序,加载的时候需要将驱动程序配置到classpath之中
2、连接数据库,通过Connection 接口和 DriverManager 类完成
3、操作数据库,通过Statement、PreparedStatement、ResultSet 三个接口完成
4、关闭数据库,在实际开发中数据库资源非常有限,操作完之后必须关闭
注:Statement 和 PreparedStatement区别
1.直接使用Statement,驱动程序一般不会对sql语句作处理而直接交给数据库;使用PreparedStatement,形成预编译的过程,并且会对语句作字符集的转换,这样就有两个好处:对于多次重复执行的语句,使用PreparedStatement效率会更高一点,并且在这种情况下页比较适合使用batch;另外,可以比较好的解决系统的本地化问题。
2.PreparedStatement还能有效的防治危险字符的注入,也就是sql注入的问题。
数据库连接操作
在JDBC 的操作中,如果要想进行数据库的连接,则必须按照以上的几步完成
1、通过Class.forName()加载数据库的驱动程序
2、通过DriverManager 类进行数据库的连接,连接的时候要输入数据库的连接地址、用户名、密码
3、通过Connection 接口接收连接
JDBC-ODBC桥连方式
/*
* 使用jdbc-odbc桥连操作数据库
* 1.配置数据源
* 2.连接数据源
*/
package demoSQL;
import java.sql.*;
public class testODBC
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Connection ct = null;
Statement st=null;
ResultSet rs=null;
PreparedStatement ps=null;
try{
//1.加载驱动(作用是把需要的驱动程序加入内存)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2.得到链接【指定连接到哪个数据源】
ct=DriverManager.getConnection("jdbc:odbc:mytest");
//3.创建Statement 或者 PreparedStatement
//Statement作用是:主要用于发送sql语句到数据库
st=ct.createStatement();
//executeUpdate 可以执行添加,删除,修改,返回的结果是sql语句执行完对表影响的行数
if(st.executeUpdate("insert into hero values(6,'关羽','男',null)")==1){
System.out.println("执行成功");
}else {
System.out.println("执行错误");
}
//查询
//ResultSet 结果集,可以理解成一个表行的结果集
rs = st.executeQuery("select * from hero");
//rs指向结果集的第一行的前一行
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
}
ps= ct.prepareStatement("select * from hero where heroID=?");
ps.setInt(1, 5);
rs = ps.executeQuery();
System.out.println();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
}
}catch (Exception e) {
// TODO: handle exception
System.out.println("error");
e.printStackTrace();
}finally{
//关闭资源
//关闭资源顺序是,谁后创建,谁先关闭
try
{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(st!=null){
st.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
JDBC直接连
package demoSQL;
import java.sql.*;
public class TestJDBC1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection cn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// cn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=aaa;user=sa;password=111111");
cn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=aaa;integratedSecurity=true");
ps=cn.prepareStatement("select * from hero");
rs=ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
ps=cn.prepareStatement("create table tmp (name nvarchar(30))");
//执行ddl语句
if(!ps.execute()){
System.out.println("建表成功");
}else{
System.out.println("建表失败");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(cn!=null){
cn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}