• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Neil_Ling

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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();
   }
  }
  
 }
}

 

posted on 2014-10-14 15:44  Neil_Ling  阅读(774)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3