定义了一些接口(java.sql包),各个数据库厂商实现接口。
Connection,Statement,ResultSet
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class TestJDBC { public static void main(String[] args) { Connection con = null; Statement sta = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("驱动程序加载完毕!"); String url = "jdbc:sqlserver://localhost:1433;databaseName=pas"; con = DriverManager.getConnection(url, "sa", "sa"); System.out.println(con); sta = con.createStatement(); String sql = "insert into teacher(teanum, teaName, teatitle, teatypeid, teabtd, pwd, static) values ('20210001', '张三', '老师', 1, '1999-9-13', 1, 1)"; //String sql2 = "delete from teacher where teaNum = '20210001'"; //sql语句影响的行数 int rows = sta.executeUpdate(sql); if (rows == 1) { System.out.println("op success"); } else { System.out.println("op error"); } } catch (ClassNotFoundException e) { System.out.println("驱动程序没有找到"); e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if(sta != null) { sta.close(); sta = null; } if(con != null) { con.close(); con = null; } } catch (SQLException e) { e.printStackTrace(); } } System.out.println("后面代码"); } }