1、添加
package pers.Pre.add; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class Add { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/student mangement system"; String username = "root"; String password = "root"; con = DriverManager.getConnection(url, username, password); String insertSql = "insert into score(studentno,courseno,usually,final)values(?,?,?,?)"; ps = con.prepareStatement(insertSql);// 获取预处理对象 ps.setString(, "); ps.setString(, "b221"); ps.setDouble(, ); ps.setDouble(, ); int num = ps.executeUpdate(); System.out.println("添加了" + num + "条记录"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } } }
2、删除
package pers.Pre.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class DeleteDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/student mangement system"; String username = "root"; String password = "root"; con = DriverManager.getConnection(url, username, password); String deleteSql = "Delete from Student where sname=?"; ps = con.prepareStatement(deleteSql);// 获取预处理对象 ps.setString(,"秀儿"); int num = ps.executeUpdate(); System.out.println("删除了" + num + "条信息"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } } }
3、修改
package pers.Pre.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class UpdateDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/student mangement system"; String username = "root"; String password = "root"; con = DriverManager.getConnection(url, username, password); String updateSql = "Update Login set password=? where account ='学前班秀儿'"; ps = con.prepareStatement(updateSql);// 获取预处理对象 ps.setString(,"); int num = ps.executeUpdate(); System.out.println("更改了" + num + "条信息"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } } }
4、查询
package pers.Pre.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class SelectDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/student mangement system"; String username = "root"; String password = "root"; con = DriverManager.getConnection(url, username, password); String selectSql = "select * from score"; ps = con.prepareStatement(selectSql);// 获取预处理对象 rs = ps.executeQuery(); System.out.println(" "+"学号"+" "+"班级"+" "+"平时成绩"+" "+"期末成绩"); while (rs.next()) { System.)+)+)+" "+ rs.getString()); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) ps.close(); if (con != null) con.close(); if (rs!=null) rs.close(); } catch (Exception e) { e.printStackTrace(); } } } }
5、Statement接口与PreparedStatement接口的对比
Statement接口用于处理不带参数的静态sql语句,PreparedStatement接口可以处理带参数的sql语句。