zoukankan      html  css  js  c++  java
  • JDBC对数据库表格的操作

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class demo1 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    //       查看 
            findAll();      
    //        增加
            inset();
    //        删除
    //        delet();
            //更新
    //        gengxin();
        }
        //更新 
        public static void gengxin(){
             Connection connection=null;
             Statement statement=null;
            
             String url="jdbc:mysql://localhost:3306/epet";
             String user="root";
             String password="root";
            String sql = "UPDATE master SET name='ljy',password='gggg',money=1000 WHERE id=118";
             //驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
             //获得连接对象
             try {
                connection=DriverManager.getConnection(url, user, password);
            
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    //            获取Statement
             try {
                statement = connection.createStatement();
                statement.executeUpdate(sql);
            
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
             
             
             try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        
    //    删除
        public static void delet(){
             Connection connection=null;
             Statement statement=null;
            
             String url="jdbc:mysql://localhost:3306/epet";
             String user="root";
             String password="root";
             String sql = "DELETE FROM master WHERE id=117";
             
             //驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
             //获得连接对象
             try {
                connection=DriverManager.getConnection(url, user, password);
            
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    //            获取Statement
             try {
                statement = connection.createStatement();
                statement.executeUpdate(sql);
            
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
             
             
             try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        //查看全部
        public static void findAll(){
        Connection  connection =null;
         Statement statement=null;
         ResultSet resultSet= null;
         String url="jdbc:mysql://localhost:3306/epet";
         String user="root";
         String password="root";
         String sqlsString = "SELECT * FROM master";
         //驱动
         try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection(url, user, password);
            statement =connection.createStatement();
            resultSet =statement.executeQuery(sqlsString);
            while(resultSet.next()){
                
                System.out.println(resultSet.getObject("id")+"	");
                System.out.println(resultSet.getObject("name")+"	");
                System.out.println(resultSet.getObject("password")+"	");
                System.out.println(resultSet.getObject("money")+"	");
                System.out.println("*******************************");
                
            }
            
        } catch (SQLException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                if(null !=resultSet){
                    resultSet.close();
                }if(null != statement){
                    statement.close();
                }
                if(null !=connection ){
                    connection.close();
                }
                
                
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
         
        
        
         
        }
        
    //增加
         public  static void  inset(){
             Connection connection=null;
             Statement statement=null;
            
             String url="jdbc:mysql://localhost:3306/epet";
             String user="root";
             String password="root";
             String sqlString="INSERT INTO master(name,password,money) VALUES ('zhh','v5',10000000)";
             
             //驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
             //获得连接对象
             try {
                connection=DriverManager.getConnection(url, user, password);
                System.out.println("连接成功");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    //            获取Statement
             try {
                statement = connection.createStatement();
                statement.executeUpdate(sqlString);
                System.out.println("添加成功");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
             
             
             try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
             
             
         }
        
        
         
         
    }
  • 相关阅读:
    自建 IPA 分发平台
    一个优雅的占位图解决方案。适用于 UITableView 和 UICollectionView。
    Vuejs2.0购物车和地址选配学习笔记
    UIWebView 加 MJRefresh 同时解决底部黑影问题
    UIWebView 不自动全屏播放视频
    左右分页按钮的集合视图控件。用于快速编写出集合视图上分页多按钮点击事件!
    课程总结
    IO流实训
    事件处理
    变色
  • 原文地址:https://www.cnblogs.com/zhv5/p/6168497.html
Copyright © 2011-2022 走看看