1.加载驱动 Class.forName("com.mysql.jdbc.Driver")
2.连接数据库(connection conn = DriverManager.getConnection())
3.预执行(创建statement,执行sql语句)
//statement stmt = conn.createStatement()
prepareStatement ps = conn.prepareStatement()
4.执行(查看结果)
ResultSet rs = stmt.executeQuery(excuteUpdate)(sql)
5.释放资源
rs.close()
ps.close()
//stmt.close()
conn.close()
删除操作:
1 package cn.bdqn; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.SQLException; 7 8 public class Jdbc_test02 { 9 public static void main(String[] args) { 10 Connection conn = null; 11 PreparedStatement ps = null; 12 int rs = 0; 13 //加载驱动 14 try { 15 Class.forName("com.mysql.jdbc.Driver"); 16 //连接数据库 17 String url = "jdbc:mysql://localhost:3306/test2"; 18 conn = DriverManager.getConnection(url, "root", "123456"); 19 //预连接(执行sql语句) 20 //删除操作 21 String sql = "DELETE from student where sno = 108"; 22 ps = conn.prepareStatement(sql); 23 //执行(查看结果) 24 rs = ps.executeUpdate(); 25 if(rs > 0) { 26 System.out.println("删除成功"); 27 }else { 28 System.out.println("删除失败"); 29 } 30 } catch (ClassNotFoundException e) { 31 e.printStackTrace(); 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } finally { 35 try { 36 //释放资源 37 if(ps != null) { 38 ps.close(); 39 } 40 if(conn != null){ 41 conn.close(); 42 } 43 } catch (SQLException e) { 44 e.printStackTrace(); 45 } 46 } 47 } 48 }
查询操作:
1 package cn.bdqn; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class Jdbc_test05 { 10 public static void main(String[] args) { 11 //1.加载驱动 Class.forName("com.mysql.jdbc.Driver") 12 13 //2.连接数据库(connection conn = DriverManager.getConnection()) 14 15 //3.预执行(创建statement,执行sql语句) 16 //statement stmt = conn.createStatement() 17 //prepareStatement ps = conn.prepareStatement() 18 19 //4.执行(查看结果) 20 //ResultSet rs = stmt.executeQuery(excuteUpdate)(sql) 21 //循环遍历 22 23 //5.释放资源 24 //rs.close() 25 //stmt.close() 26 //conn.close() 27 28 29 Connection conn = null; 30 PreparedStatement ps = null; 31 ResultSet rs = null; 32 //1.加载驱动 Class.forName("com.mysql.jdbc.Driver") 33 try { 34 Class.forName("com.mysql.jdbc.Driver"); 35 //2.连接数据库(connection conn = DriverManager.getConnection()) 36 String url = "jdbc:mysql://localhost:3306/test2"; 37 conn = DriverManager.getConnection(url, "root", "123456"); 38 //3.预执行(创建statement,执行sql语句) 39 String sql = "SELECT sno,sname,Sbirthday,class from student"; 40 ps = conn.prepareStatement(sql); 41 //4.执行(查看结果) 42 rs = ps.executeQuery(); 43 while(rs.next()) { 44 System.out.println(rs.getInt("sno")+"\t"+rs.getString("sname")+"\t"+rs.getDate("Sbirthday")+"\t"+rs.getInt("class")); 45 } 46 } catch (ClassNotFoundException e) { 47 e.printStackTrace(); 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 } finally { 51 try { 52 //释放资源
if(rs != null){
rs.close();
}
53 if(ps != null) { 54 ps.close(); 55 } 56 if(conn != null) { 57 conn.close(); 58 } 59 } catch (SQLException e) { 60 e.printStackTrace(); 61 } 62 } 63 } 64 }
对于增,删,改操作 int rs = ps.executeUpdate(sql);
查询操作 resultSet rs = ps.executeQuery();