zoukankan      html  css  js  c++  java
  • java jdbc----mysql的select、insert、update、delete

    //-----------------------------------select----------------------------------
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class TestSelect{
     public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;
      try {
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager
         .getConnection("jdbc:mySql://localhost/mydata?user=root&password=root");
       stmt = conn.createStatement();
       String sql = "select * from article";
       rs = stmt.executeQuery(sql);
       while (rs.next()) {
        System.out.println(rs.getInt("id"));
        System.out.println(rs.getString("title"));
       }
      } catch (Exception e) {
       e.printStackTrace();
      } finally {
       try {
        if (rs != null) {
         rs.close();
         rs = null;
        }
        if (stmt != null) {
         stmt.close();
         stmt = null;
        }
        if (conn != null) {
         conn.close();
         conn = null;
        }
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
     }
    }
    //-------------------------insert--------------------------
    import java.sql.*;
    public class TestInsert {
     public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try {
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager
         .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
       stmt = conn.createStatement();
       String sql = "insert into article(title) values('fff')";
       stmt.executeUpdate(sql);
      } catch (Exception e) {
       e.printStackTrace();
      } finally {
       try {
        stmt.close();
        conn.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
     }
    }
    //-----------------------------update----------------------------
    import java.sql.*;
    public class TestUpdate {
     public static void main(String[]args){
      Connection conn = null;
      Statement stmt = null;
      try{
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
       stmt = conn.createStatement();
       String sql = "update article set title ='ggg' where id=5";
       stmt.executeUpdate(sql);
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       try {
        stmt.close();
        conn.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
     }
    }
    //---------------------------delete-------------------------
    import java.sql.*;
    public class TestDelete {
     public static void main(String[]args){
      Connection conn = null;
      Statement stmt = null;
      try{
       Class.forName("com.mysql.jdbc.Driver");
       conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
       stmt = conn.createStatement();
       stmt.execute("delete from article where title='fff'");
       
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       try {
        conn.close();
        stmt.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
      
     }
    }
  • 相关阅读:
    【bzoj2962】序列操作 线段树
    【bzoj1922】[Sdoi2010]大陆争霸 堆优化Dijkstra
    .NET Core / C# 开发 IOT 嵌入式设备的个人见解
    C#中Equals和= =(等于号)的比较)
    VS 2017常用快捷键
    【你不一定知晓的】C#取消异步操作
    工程实践:给函数取一个"好"的名字
    接口测试入门篇
    博客园知名博主 Vamei 英年早逝!
    人生苦短,我用 Python
  • 原文地址:https://www.cnblogs.com/NeilLing/p/4024558.html
Copyright © 2011-2022 走看看