为什么要JDBC?:因为sql语句能直接访问数据库 而Java不能直接访问
JDBCJava开发人员而言是API,对数据库提供商而言是接口
JDBC驱动器根据其实现方式分为4种类型 :
1、JDBC-ODBC bridge plus ODBC driver: JDBC-ODBC桥驱动程序,将JDBC调用转换为ODBC的调用。( This combination provides JDBC access via ODBC drivers. ODBC binary code--and in many cases, database client code-- must be loaded on each client machine that uses a JDBC-ODBC Bridge. Sun provides a JDBC-ODBC Bridge driver, which is appropriate for experimental use and for situations in which no other driver is available.)注意, 必须将ODBC二进制代码(许多情况下还包括数据库客户机代码)加载到使用该驱动程序的每个客户机上。这种类型的驱动程序最适合于企业网(这种网络上客户机的安装不是主要问题),或者是用Java编写的三层结构的应用程序服务器代码。
2、Native-API partly-Java driver: 将JDBC调用转换为对数据库客户端API的调用。(A native-API partly Java technology-enabled driver: This type of driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.)这种类型的驱动程序把客户机API上的JDBC调用转换为Oracle、Sybase、Informix、DB2或其它DBMS的调用。注意,象桥驱动程序一样,这种类型的驱动程序要求将某些二进制代码加载到每台客户机上。
3、 Pure Java Driver for Database Middleware: 先将JDBC调用转换为DBMS-independent网络协议,然后由服务器端的中间件转换为具体数据库服务器可以接收的网络协议。(net-protocol fully Java technology-enabled driver #This style of driver translates JDBC calls into the middleware vendor's protocol, which is then translated to a DBMS protocol by a middleware server. The middleware provides connectivity to many different databases.)这种网络服务器中间件能够将它的纯Java客户机连接到多种不同的数据库上。所用的具体协议取决于提供者。通常,这是最为灵活的JDBC驱动程序。有可能所有这种解决方案的提供者都提供适合于Intranet用的产品。为了使这些产品也支持Internet访问,它们必须处理Web所提出的安全性、通过防火墙的访问等方面的额外要求。几家提供者正将JDBC驱动程序加到他们现有的数据库中间件产品中。
4、Direct-to-Database Pure Java Driver: 将JDBC调用直接转换为具体数据库服务器可以接收的网络协议。(native-protocol fully Java technology-enabled driver #This style of driver converts JDBC calls into the network protocol used directly by DBMSs, allowing a direct call from the client machine to the DBMS server and providing a practical solution for intranet access.)这将允许从客户机机器上直接调用DBMS服务器,是Intranet访问的一个很实用的解决方法。由于许多这样的协议都是专用的,因此数据库提供者自己将是主要来源,有几家提供者已在着手做这件事了。
package com.wode.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/***
*
* @author Administrator
*查找数据库中的数据
*/
public class Seek {
public static void main(String[] args) {
//首先获得配置文件里的信息
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./sql.properties"));
String name = properties.getProperty("name");
String pwd = properties.getProperty("pwd");
String url = properties.getProperty("url");
//通过反射获得驱动
Class.forName("com.mysql.jdbc.Driver");
//准备连接
Connection conn = DriverManager.getConnection(url,name,pwd);
//System.out.println(conn); 查看是否连接成功
//准备sql语句 取出qqusers 里所有的账号和密码 和用户信息
String sql = "select * from qqusers s join qquserinfo o where s.qquser_id=o.qquser_id";
//运行sql 语句
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
//取出结果
while(resultSet.next()){
String qqname = resultSet.getString("s.qquser_name");
String qqpwd = resultSet.getString("s.qquser_pwd");
String qqinfoname = resultSet.getString("o.qqinfo_nick");
int qqinfoage = resultSet.getInt("o.qqinfo_age");
System.out.println("账号:"+qqname+" 密码:"+qqpwd+" 昵称:"+qqinfoname
+" 年纪:"+qqinfoage);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.wode.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;
/**
*
* @author Administrator
*模拟QQ 用户登录
*/
public class Landing {
public static void main(String[] args) {
//在配置文件找到我们需要的东西
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./sql.properties"));
String name = properties.getProperty("name");
String pwd = properties.getProperty("pwd");
String url = properties.getProperty("url");
//反射驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection connection = DriverManager.getConnection(url,name,pwd);
//用户输入账号 密码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账号");
String uName = scanner.next();
System.out.println("请输入密码");
String uPwd = scanner.next();
//准备sql 语句
String sql = "SELECT COUNT(*) FROM qqusers where qquser_name=? and qquser_pwd=?";
//预编译sql 语句 在这里使用 Statement 会有很大的风险 别人可以通过特殊的方法破坏数据库
//所以我们使用 PreparedStatement 这里就将sql 语句传入 表示出预编译
//只有在静态的sql 语句才会使用 Statement 而 动态的sql 语句必须使用 PreparedStatement
PreparedStatement ptmt = connection.prepareStatement(sql);
ptmt.setString(1, uName);//这里的 1 表示第一个?号的下标
ptmt.setString(2, uPwd);
//接收结果
ResultSet resultSet = ptmt.executeQuery();
if(resultSet.next()){//如果有下一个 进入if
System.out.println(resultSet.getInt(1));//getInt(1) 访问下标为1 的结果
}
int a = resultSet.getInt(1);
if (a!=1) {
System.out.println("您输入的账号或密码错误");
}else if(a==1) {
System.out.println("登录成功");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.wode.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @author Administrator
*增加数据库的的数据
*/
public class Increase {
public static void main(String[] args) {
//首先获取配置文件的信息
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./sql.properties"));
String name = properties.getProperty("name");
String pwd = properties.getProperty("pwd");
String url = properties.getProperty("url");
//反射驱动
Class.forName("com.mysql.jdbc.Driver");
//准备连接
Connection connection = DriverManager.getConnection(url,name,pwd);
//准备sql 语句
String sql ="insert into qqusers values(null,'789','123',1)";
//运行sql 语句
Statement statement = connection.createStatement();
//使用int 类型接受结果 如果为1 那么证明运行成功 为0 则表示数据库没变化
int resultSet = statement.executeUpdate(sql);
System.out.println(resultSet);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.wode.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.imageio.stream.FileImageInputStream;
/**
*
* @author Administrator
*删除数据库的数据
*/
public class DeleteDB {
public static void main(String[] args) {
//配置文件找到我们需要的东西
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./sql.properties"));
String name = properties.getProperty("name");
String pwd = properties.getProperty("pwd");
String url=properties.getProperty("url");
//反射驱动
Class.forName("com.mysql.jdbc.Driver");
//准备连接
Connection connection = DriverManager.getConnection(url,name,pwd);
//准备sql 语句
String sql = "DELETE FROM qqusers where qquser_id=7";
//运行sql语句
Statement statement = connection.createStatement();
int resultSet = statement.executeUpdate(sql);
System.out.println(resultSet);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.wode.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.acl.Permission;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/***
*
* @author Administrator
*改变数据库里的数据
*/
public class Change {
public static void main(String[] args) {
//在配置文件找到我们需要的东西
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./sql.properties"));
String name = properties.getProperty("name");
String pwd = properties.getProperty("pwd");
String url = properties.getProperty("url");
//反射驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
Connection connection = DriverManager.getConnection(url,name,pwd);
//准备sql 语句
String sql = "UPDATE qqlog SET qqlog_time='2016-09-09 00:00:00' WHERE qqlog_id=2";
//运行sql语句
Statement statement = connection.createStatement();
int resultSet = statement.executeUpdate(sql);
System.out.println(resultSet);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//是我的配置文件 sql.properties
批量处理:
conn.setAutoCommit(false); //取消自动提交
Statement stm = conn.cerate Statement();
stm.addBatch(); //批量处理语句
stm.executeBatch(); //执行批量处理语句
conn.commit(); //手动提交
conn.setAutoCommit(true); //最后在打开自动提交