如图:
左侧 -- 这种风格的驱动程序将JDBC调用 直接转换成 数据库管理系统使用的网络协议,通过Intranet访问直接从客户机调用DBMS服务器。package org.gjt.mm.mysql; import java.sql.SQLException; public class Driver extends com.mysql.jdbc.Driver { }
public class Driver extends NonRegisteringDriver implements java.sql.Driver { static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } } }
public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; try { // 加载 驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 试图建立到给定数据库 URL 的连接。DriverManager 试图从已注册的 JDBC 驱动程序集中选择一个适当的驱动程序。 conn = DriverManager.getConnection(url, user, password); System.out.println(conn); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; Statement st = null; try { // 加载 驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 试图建立到给定数据库 URL 的连接。DriverManager 试图从已注册的 JDBC 驱动程序集中选择一个适当的驱动程序。 conn = DriverManager.getConnection(url, user, password); // 创建一个 Statement 对象来将 SQL 语句发送到数据库。 st = conn.createStatement(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) { st.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; Statement st = null; try { // 加载 驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 试图建立到给定数据库 URL 的连接。DriverManager 试图从已注册的 JDBC 驱动程序集中选择一个适当的驱动程序。 conn = DriverManager.getConnection(url, user, password); // 创建一个 Statement 对象来将 SQL 语句发送到数据库。 st = conn.createStatement(); // 执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。 ResultSet rs = st.executeQuery("select * from user"); // 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。 // ResultSet 对象具有指向其当前数据行的光标。最初,光标被置于第一行之前。next 方法将光标移动到下一行;因为该方法在 // ResultSet 对象没有下一行时返回 false,所以可以在 while 循环中使用它来迭代结果集。 while (rs.next()) { // 在对每一行进行处理时,可以对各个列按任意顺序进行处理。 // 不过,按从左至右的顺序对各列进行处理可以获得较高的执行效率。 System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) { st.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
Demo:
public static void main(String[] args) throws FileNotFoundException, InterruptedException { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; PreparedStatement ps = null; try { PrintWriter writer = new PrintWriter(new File("error.txt")); DriverManager.setLogWriter(writer); Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); ps = conn.prepareStatement("select * from user"); // 执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。 ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (ps != null) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args) throws FileNotFoundException, InterruptedException { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; CallableStatement pc = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); pc = conn.prepareCall("{call getUser()}"); ResultSet rs = pc.executeQuery(); while (rs.next()) { System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pc != null) { pc.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args) throws FileNotFoundException, InterruptedException { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; CallableStatement pc = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); pc = conn.prepareCall("{call getUser()}"); ResultSet rs = pc.executeQuery(); ResultSetMetaData metaData = rs.getMetaData(); for (int i = 0; i < metaData.getColumnCount(); i++) { System.out.println(metaData.getColumnName(i + 1)); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pc != null) { pc.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args) throws FileNotFoundException, InterruptedException { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; Statement st = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); st = conn.createStatement(); int count = st .executeUpdate("insert into user (username,password) values ('yes','233')"); System.out.println("影响行数:" + count); st.executeUpdate("delete from user where id = 4"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) { st.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args) throws FileNotFoundException, InterruptedException { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); ps = conn.prepareStatement("select * from user where username = ?"); ps.setString(1, "hello"); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (ps != null) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args){ String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); conn.setAutoCommit(false); ps = conn.prepareStatement("update user set money = ? where id = ?"); ps.setDouble(1, 8); ps.setInt(2, 1); ps.executeUpdate(); // int i = 1/0; ps.setDouble(1, 12); ps.setInt(2, 2); ps.executeUpdate(); conn.commit(); } catch (Exception e) { if(conn != null){ try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } finally { try { if (ps != null) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/jdbc_test"; String user = "root"; String password = "123456"; Connection conn = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); ps = conn .prepareStatement("update user set money = ? where id = ?"); ps.setDouble(1, 10); ps.setInt(2, 1); ps.addBatch(); ps.setDouble(1, 10); ps.setInt(2, 2); ps.addBatch(); ps.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }