MySql用statement實現DDL,DML,DQL的操作Demo

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   os   sp   for   資料   

Demo1

Connection connection=null;        Statement stmt=null;        int result=-1;                try {            Class.forName("com.mysql.jdbc.Driver");                    } catch (ClassNotFoundException e) {            e.printStackTrace();        }                        try {            //建立串連            String url="jdbc:mysql://localhost:3306/jdbcdb";            connection= DriverManager.getConnection(url, "root", "mysql");        } catch (SQLException e) {            e.printStackTrace();        }                try {            //建立Statement            String sql="CREATE TABLE s_user(id INT PRIMARY KEY AUTO_INCREMENT,    NAME VARCHAR(20),PASSWORD VARCHAR(15)) ";             stmt=connection.createStatement();             //執行sql語句,返回受影響行數 ————int值             result= stmt.executeUpdate(sql);        } catch (SQLException e) {            e.printStackTrace();        }                System.out.println("result="+result);                try {            //關閉流            if(stmt!=null)            {                stmt.close();            }                        if(connection!=null)            {                connection.close();            }                    } catch (SQLException e) {            e.printStackTrace();        }

Demo2

Connection connection=null;        Statement stmt=null;        ResultSet rSet=null;        try {            Class.forName("com.mysql.jdbc.Driver");            String url="jdbc:mysql://localhost:3306/jdbcdb";            String user="root";            String password="mysql";            //串連            connection= DriverManager.getConnection(url, user, password);            stmt= connection.createStatement();//statement            String sql="SELECT * from s_user;";            rSet= stmt.executeQuery(sql);//執行sql語句---資料集(類似於map)                        while (rSet.next()) {                //擷取值(通過索引)                int id= rSet.getInt(1);                String name=rSet.getString(2);                String pwd=rSet.getString(3);                                System.out.println("id="+id+";name="+name+";pwd="+pwd);                //通過行列號                id=rSet.getInt("id");                name=rSet.getString("name");                pwd=rSet.getString("password");                                System.out.println("~~~~~~~id="+id+";name="+name+";pwd="+pwd);                            }                    } catch

 

MySql用statement實現DDL,DML,DQL的操作Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.