JDBC(Java Data Base Connectivity数据库连接)是一种用于执行SQL语句的java API,可以为多种关系数据库提供统一访问,它是由一组用java语言编写的类和接口组成。
Connection接口
Statement接口
PreparedStatement接口
ResultSet接口
CallableStatement接口
DriverManager接口
把mysql驱动包直接粘贴到工程目录下-->选中jar包右击-->Build Path-->Add to BuildPath
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection(jdbc:mysql://地址:端口/库名","root","admin");
1、获取连接
2、获取Statement对象
Connection.createStatement()
3、整理查询的sql语句字符串
String sql="sql语句"
4、发送并执行sql语句
ResultSet rs = Statement.executeQuery(sql语句);
5、处理结果集
6、关闭连接
public static Connection getConnection(){ Connection con = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 con = DriverManager.getConnection(url, user, psw);//获取连接 } catch (Exception e) { e.printStackTrace(); } return con; }
public static void insertValue(Teacher tea){ Connection con = getConnection(); try { Statement state = con.createStatement();//获取程序接口statement对象 String sql = "insert into teacher(id,name,gender,age,job,creatDate)" + " values(?,?,?,?,?,?)";//创建sql语句对象(预制对象) PreparedStatement pstate = con.prepareStatement(sql); pstate.setInt(1, tea.getId()); pstate.setString(2, tea.getName()); pstate.setString(3, tea.getGender()); pstate.setInt(4, tea.getAge()); pstate.setString(5, tea.getJob()); pstate.setString(6, tea.getCreatDate()); pstate.executeUpdate(); state.close();//关闭程序接口 con.close();//关闭连接 System.out.println("执行完成..."); } catch (Exception e) { e.printStackTrace(); } }
这里的增加数据代码涉及到了预置对象,就是values()里面加上问号'?',可以动态添加数据对象的信息。
public static void delectValue(){ Connection con = getConnection(); try { Statement state = con.createStatement();//获取程序接口statement对象 String sql = "delete from teacher where id=2";//创建sql语句对象 int executeUpdate = state.executeUpdate(sql);//执行语句并返回受影响的行数 System.out.println(executeUpdate); state.close();//关闭程序接口 con.close();//关闭连接 System.out.println("执行完成..."); } catch (Exception e) { e.printStackTrace(); } }
public static void updateValue(){ Connection con = getConnection(); try { Statement state = con.createStatement();//获取程序接口statement对象 String sql = "update teacher set name='newName' where name='wangze'";//创建sql语句对象 int executeUpdate = state.executeUpdate(sql);//执行语句并返回受影响的行数 System.out.println(executeUpdate); state.close();//关闭程序接口 con.close();//关闭连接 System.out.println("执行完成..."); } catch (Exception e) { e.printStackTrace(); } }
public static void queryValue(){ Connection con = getConnection(); try { Statement state = con.createStatement();//获取程序接口statement对象 String sql = "select * from teacher";//创建sql语句对象 ResultSet rs = state.executeQuery(sql); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String gender = rs.getString("gender"); int age = rs.getInt("age"); String job = rs.getString("job"); Date date = rs.getDate("creatDate"); System.out.println("id="+id+" name="+name+" gender=" +gender+" age="+age+" job="+job+" date="+date); } state.close();//关闭程序接口 con.close();//关闭连接 System.out.println("执行完成..."); } catch (Exception e) { e.printStackTrace(); } }
JDBC的PPT文档
JDBC的jar包