数据库 CRUD操作

查询执行ResultSet rs = st.executeQuery(sql);

曾 删 改 都执行 st.executeUpdate(sql);

注意链接数据库要制定字符集,否则会出现中文乱码问题。

最后关闭连接,避免资源浪费。

  1. package info.dyndns.oszc.Introduce;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class JdbcUtils {  
  6.   
  7.     private static String driver = "com.mysql.jdbc.Driver";  
  8.       
  9.     public static Connection conn(String url, String user, String password){  
  10.         try{  
  11.             Class.forName(driver);  
  12.             Connection conn = DriverManager.getConnection(url,user,password);  
  13.               
  14.             return conn;  
  15.         }catch(Exception e){  
  16.             System.out.println("连接错误!");  
  17.             e.printStackTrace();  
  18.         }  
  19.         return null;  
  20.           
  21.     }  
  22.       
  23.     public static void closeDB(ResultSet rs, Statement st, Connection conn){  
  24.         if (rs!=null){  
  25.             try{  
  26.                 rs.close();  
  27.             } catch (SQLException e) {  
  28.                 e.printStackTrace();  
  29.             }finally{  
  30.                 if(st != null){  
  31.                     try{  
  32.                         st.close();  
  33.                     } catch (SQLException e) {  
  34.                         e.printStackTrace();  
  35.                     }finally{  
  36.                         if(conn!=null){  
  37.                             try{  
  38.                                 conn.close();  
  39.                             } catch (SQLException e) {  
  40.                                 e.printStackTrace();  
  41.                             }  
  42.                         }  
  43.                     }  
  44.                 }  
  45.             }  
  46.               
  47.         }  
  48.           
  49.     }  
  50. }  


  1. package info.dyndns.oszc.Introduce;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class CRUD {  
  6.       
  7.     public static void creat() throws Exception{  
  8.         String url =  "jdbc:mysql://localhost:3306/comment?characterEncoding=UTF-8";  
  9.         //指定字符集 否则增加中文会乱码  
  10.         String sql = "insert into say (name,content) values('xxx','呵呵')";  
  11.         Connection conn =  
  12.                 JdbcUtils.conn(url, "oszc""1234");  
  13.         Statement st = conn.createStatement();  
  14.         st.executeUpdate(sql);  
  15.           
  16.         JdbcUtils.closeDB(null, st, conn);  
  17.     }  
  18.       
  19.     public static void delete() throws Exception{  
  20.         String url =  "jdbc:mysql://localhost:3306/comment?characterEncoding=UTF-8";  
  21.         String sql = "DELETE FROM say WHERE id = 123";  
  22.         Connection conn =  
  23.                 JdbcUtils.conn(url, "oszc""1234");  
  24.         Statement st = conn.createStatement();  
  25.         st.executeUpdate(sql);  
  26.           
  27.         JdbcUtils.closeDB(null, st, conn);  
  28.     }  
  29.       
  30.       
  31.       
  32.     public static void update() throws Exception{  
  33.         String url =  "jdbc:mysql://localhost:3306/comment?characterEncoding=UTF-8";  
  34.         String sql = "UPDATE say SET content='正确了!' WHERE id = 125";  
  35.         Connection conn =  
  36.                 JdbcUtils.conn(url, "oszc""1234");  
  37.         Statement st = conn.createStatement();  
  38.         st.executeUpdate(sql);  
  39.       
  40.         JdbcUtils.closeDB(null, st, conn);  
  41.     }  
  42.   
  43.     public static void read() throws Exception{  
  44.         String url =  "jdbc:mysql://localhost:3306/comment";  
  45.         String sql = "select id,name,content from say";  
  46.         Connection conn =  
  47.                 JdbcUtils.conn(url, "oszc""1234");  
  48.         Statement st = conn.createStatement();  
  49.         ResultSet rs = st.executeQuery(sql);  
  50.         while (rs.next()){  
  51.             System.out.println(rs.getObject("id")+"\t"+  
  52.         rs.getObject("name")+"\t"+rs.getObject("content")+"\t");  
  53.         }  
  54.           
  55.         JdbcUtils.closeDB(rs, st, conn);  
  56.     }  
  57.   
  58.       
  59.     public static void main(String[] args) throws Exception {  
  60.         //CRUD.creat();  
  61.         //CRUD.update();  
  62.         //CRUD.delete();  
  63.         CRUD.read();  
  64.     }  
  65.   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值