那么为什么要使用JDBC,一个应用程序如果不和数据库进行连接,那这根本就不算是个应用程序,那么应用程序是不能直接对数据库进行操作的,那么就需要一个辅助工具去连接数据库,从而操作数据库...那这个辅助的工具就是JDBC了,这个仅限于JAVA应用程序...其实总体的规模就是这样:
应用程序——>JDBC——>(mysql driver——>mysql)
JDBC——>(oracle driver——>oracle)
步骤如下:
i.首先注册驱动: Class.forName("com.mysql.Driver");//这种方式是最好的,不会对具体驱动产生依赖... DriverManager.registerDriver(com.mysql.jdbc.Driver);//会产生两个相同的驱动,并会对具体驱动产生依赖... System.setProperty("jdbc.drivers","driver1:driver2");//基本不常用,所以可以不用记,一般就使用第一种就行了.. 一般用第一种就可以了; ii.建立连接 Connection conn = DriverManager.getConnection(url, user, password); Connection 是与特定数据库的连接(会话)。在连接上下文中执行 SQL 语句并返回结果。Connection 对象的数据库能够提供描述其表、所支持的 SQL 语法、存储过程、此连接功能等等的信息。此信息是使用getMetaData 方法获得的。
注:在配置 Connection 时,JDBC 应用程序应该使用适当的 Connection 方法,比如 setAutoCommit 或 setTransactionIsolation。在有可用的 JDBC 方法时,应用程序不能直接调用 SQL 命令更改连接的配置。默认情况下,Connection 对象处于自动提交模式下,这意味着它在执行每个语句后都会自动提交更改。如果禁用了自动提交模式,那么要提交更改就必须显式调用 commit 方法;否则无法保存数据库更改。
iii.创建执行SQL语句...Statement st = conn.createStatement(); st.executeQuery(sql); 2:PreparedStatement String sql = "select * from table_name where col_name=?"; PreparedStatement ps = conn.preparedStatement(sql); ps.setString(1, "col_value"); ps.executeQuery();
用于执行静态 SQL 语句并返回它所生成结果的对象。
所有已知子接口: CallableStatement, PreparedStatement iv.处理执行结果... ResultSet rs = statement.executeQuery(sql); While(rs.next()){ rs.getString(“col_name”); rs.getInt(“col_name”); }ResultSet 对象具有指向其当前数据行的光标。最初,光标被置于第一行之前。next 方法将光标移动到下一行;因为该方法在 ResultSet 对象没有下一行时返回 false,所以可以在 while 循环中使用它来迭代结果集。
默认的 ResultSet 对象不可更新,仅有一个向前移动的光标。因此,只能迭代它一次,并且只能按从第一行到最后一行的顺序进行。可以生成可滚动和/或可更新的 ResultSet 对象。
v.释放资源...释放资源这是必须的,使用close()进行关闭...由于数据库的资源是非常珍贵的,因此只要我们不去使用资源的时候,一定要记得释放...
实例如下:
import java.sql.*;//1.引包是必须的... public class JDBC_1_1 { static final String JDBC_DRIVER="com.mysql.jdbc.Driver"; static final String DB_URL ="jdbc:mysql://localhost:3306/emp";//这个链接是我本机的数据库emp...emp里有个表格叫employees.... /*表格的信息 *create table employees *( * id int not null, * first varchar(255) not null, * last varchar(255) not null, * age int not null *); */ static final String USER="root";//登陆数据库时的用户名... static final String PAS="49681888";登陆时的密码... public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; Statement stmt=null;// try { //2.注册JDBC驱动程序... Class.forName("com.mysql.jdbc.Driver"); //3.打开一个链接... System.out.println("Connection database...."); conn=DriverManager.getConnection(DB_URL,USER,PAS); //4.执行一个操作... System.out.println("Creating statement"); stmt=conn.createStatement(); // String sql; // sql="select id,first,last,age from employees"; 这个是查询操作... // ResultSet rs=stmt.executeQuery(sql); String sql_1="insert into employees " + "values (7, 'z', 'yh', 20)";//插入操作... stmt.executeUpdate(sql_1); 5.提取数据... // while(rs.next()){ //rs用来保存sql执行后的结果... // int id=rs.getInt("id"); // int age=rs.getInt("age"); // String first=rs.getString("first"); // String last=rs.getString("last"); // System.out.println(id+" "+age+" "+first+" "+last); // } 6.清理环境 // rs.close(); stmt.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("no class"); }finally{ if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } System.out.println("Goodbye"); } }
