JDBC简介
JDBC的工作原理
JDBC API
连接数据库
JDBC驱动
使用JDBC-ODBC桥方式连接数据库
使用纯Java方式连接数据库
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.apache.log4j.Logger; public class Conn { private static Logger logger = Logger.getLogger(Conn.class.getName()); public static void main(String[] args) { Connection conn = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { logger.error(e); } // 2.建立连接 try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000"); System.out.println("建立连接成功!"); } catch (SQLException e) { logger.error(e); } finally { // 3.关闭连接 try { if (conn != null) { conn.close(); System.out.println("关闭连接成功!"); } } catch (SQLException e) { logger.error(e); } } } }
Statement和ResultSet
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; public class Insert { private static Logger logger = Logger.getLogger(Insert.class.getName()); public static void main(String[] args) { Connection conn = null; Statement stmt = null; String name = "欧欧"; int health = 100; int love = 60; String strain = "酷酷的雪纳瑞"; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { logger.error(e); } // 2.建立连接 try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000"); stmt = conn.createStatement(); // insert into dog(name,health,love,strain) // value('欧欧',100,60,'酷酷的雪纳瑞'); StringBuffer sql = new StringBuffer( "insert into dog(name,health,love,strain) value('"); sql.append(name + "',"); sql.append(health + ","); sql.append(love + ",'"); sql.append(strain + "')"); stmt.execute(sql.toString()); logger.info("插入狗狗信息成功!"); } catch (SQLException e) { logger.error(e); } finally { // 3.关闭连接 try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { logger.error(e); } } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; public class Update { private static Logger logger = Logger.getLogger(Update.class.getName()); public static void main(String[] args) { Connection conn = null; Statement stmt = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { logger.error(e); } // 2.建立连接 try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000"); stmt = conn.createStatement(); stmt.executeUpdate("update dog set health=80,love=15 where id=1"); logger.info("成功更新了狗狗信息!"); } catch (SQLException e) { logger.error(e); } finally { // 3.关闭连接 try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { logger.error(e); } } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; public class Query { private static Logger logger = Logger.getLogger(Query.class.getName()); public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { logger.error(e); } // 2.建立连接 try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000"); stmt = conn.createStatement(); rs = stmt .executeQuery("select id,name,health,love,strain from dog"); while (rs.next()) { System.out.println(rs.getInt(1)); System.out.println(rs.getString(2)); System.out.println(rs.getInt("health")); System.out.println(rs.getInt("love")); System.out.println(rs.getString("strain")); } logger.info("查询狗狗信息成功!"); } catch (SQLException e) { logger.error(e); } finally { // 3.关闭连接 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { logger.error(e); } } } }
PreparedStatement接口
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.log4j.Logger; public class Update2 { private static Logger logger = Logger.getLogger(Update2.class.getName()); public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; // 1.加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { logger.error(e); } // 2.建立连接 try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/epet", "epetadmin", "0000"); String sql = "update dog set health=?,love=? where id=?"; pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 80); pstmt.setInt(2, 15); pstmt.setInt(3, 1); pstmt.executeUpdate(); logger.info("成功更新了狗狗信息!"); } catch (SQLException e) { logger.error(e); } finally { // 3.关闭连接 try { if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { logger.error(e); } } } }