JDBC开发步骤

时间:2022-02-11 04:42:50

 1.抛给 java 虚拟机

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Demo01 { public static void main(String[] args) throws ClassNotFoundException, SQLException { //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver"); //2.连接数据库(获取连接对象)
        String url="jdbc:mysql://127.0.0.1:3306/java 1018?characterEncoding=UTF-8"; String user="root"; String password="123456"; Connection con = DriverManager.getConnection(url,user,password); //System.out.println(con); //3.获得语句执行对象
        Statement sta = con.createStatement(); String sql = "insert into sort(sname) values(‘小裙子‘)"; //4.执行sql语句
        int row= sta.executeUpdate(sql); System.out.println(row); //5.释放资源(先开的后关)
 sta.close(); } }

 2.try……catch……

package com.oracle.demo01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class Demo05 { public static void main(String[] args) { PreparedStatement pst=null; Connection con=null; //1.注册驱动
        try { Class.forName("com.mysql.jdbc.Driver"); //2.获取连接对象
            String url="jdbc:mysql://127.0.0.1:3306/java 1018?characterEncoding=UTF-8"; String user="root"; String password="123456"; con = DriverManager.getConnection(url,user,password); //3.获取语句执行对象
            String sql="update sort set sname=? where sid=?"; pst=con.prepareStatement(sql); pst.setInt(2, 5); pst.setString(1, "小裙子"); //4.执行sql语句
            int row =pst.executeUpdate(); System.out.println(row); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }finally { //5.释放资源
            try { pst.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

3.封装为静态类

package com.oracle.demo01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JDBCUtils { //获取connection对象的方法
    public static Connection getconn() { //1.注册驱动
        Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); //2.连接数据库(获取连接对象)
            String url="jdbc:mysql://127.0.0.1:3306/java 1018?characterEncoding=UTF-8"; String user="root"; String password="123456"; conn = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return conn; } }

4.静态类调用

package com.oracle.demo01; import java.sql.PreparedStatement; import java.sql.SQLException; public class Demo06 { public static void main(String[] args) { String sql="insert into sort values(?,?)"; PreparedStatement pst=null; try { pst=JDBCUtils.getconn().prepareStatement(sql); pst.setInt(1, 4); pst.setString(2, "小裙子"); int row =pst.executeUpdate(); System.out.println(row); } catch (SQLException e) { e.printStackTrace(); }finally { try { pst.close(); } catch (SQLException e) { e.printStackTrace(); } } }
  
    //增、删、改 释放资源
    public static void close(Statement pst,Connection conn) { if (pst!=null) { try { pst.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } //查询 释放资源
    public static void close(ResultSet rs,Statement pst,Connection conn) { if (rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pst!=null) { try { pst.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
}

.