博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySql用statement实现DDL,DML,DQL的操作Demo
阅读量:7103 次
发布时间:2019-06-28

本文共 2428 字,大约阅读时间需要 8 分钟。

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

 

转载于:https://www.cnblogs.com/liuwt365/p/4095994.html

你可能感兴趣的文章
Spark环境搭建遇到的问题
查看>>
(译)你应该知道的jQuery小技巧
查看>>
关于Boot应用中集成Spring Security你必须了解的那些事
查看>>
绑定到列表的指定元素
查看>>
Android RecyclerView使用GridLayoutManager导致间隙变大的问题
查看>>
如何使用jackson美化输出json/xml
查看>>
如何实现测试系统题目的自动推荐?
查看>>
JAVA通过oshi获取系统和硬件信息
查看>>
oracle的case when的用法和decode函数的用法
查看>>
在Unity中对Lua进行调试
查看>>
01-老马jQuery教程-jQuery入口函数及选择器
查看>>
查看端口被占用的情况以及如何解除端口占用
查看>>
matlab练习程序(最终腐蚀)
查看>>
新年互联网领头行业随想,互联网营销
查看>>
华尔街日报:网络广告销售为病毒敞开大门
查看>>
利用 ICallbackEventHandler接口 实现客户端回调
查看>>
javascript模板系统 ejs v9
查看>>
C#在多种条件下选择自己的路-swith结构
查看>>
C#索引器-示例代码
查看>>
面试题 把字符串中的\t转换为4 的空格
查看>>