mysql

作者: 杜艳_66c4 | 来源:发表于2020-05-21 11:42 被阅读0次

    1、 create database 库名 create table 表名()
    2、select 2+4..可以计算和
    3、insert into tablename values('','')
    4、分页: select * from tablename desc limit 3,3, 从第3个往后的3个元素
    5、datetime : select now()
    6、表结构:desc tablename
    7、mysql数据库连接jdbc
    a. 准备工作
    IntelliJ IDEA
    mysql ——products ——mysql-connector-java-5.0.8-bin(不是最新版本)
    建立数据库 customer
    建表 customer
    b.定义数据库信息

    //数据库地址
        private static final String url = "jdbc:mysql://localhost:3306/customer";
        private static final String name = "com.mysql.jdbc.Driver";
        private static final String username = "这里填上你的数据库名称";
        private static final String password = "这里填上你的数据库的密码";
        private Connection connection = null;  
        private PreparedStatement  preparedStatement =null;
    

    c.开启连接

    private DBManager(String sql){
            try{
                Class.forName(name);
                connection = DriverManager.getConnection(url, username, password);
                preparedStatement = connection.prepareStatement(sql);
    
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    

    d.进行操作后需要的关闭连接

    private void close(){
            try{
                this.connection.close();
                this.preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    

    e. 写个demo

    public static void main(String[] args){
            String sql = "SELECT * FROM customer";
            DBManager dbManager = new DBManager(sql);  //实例化
    
            String id, name, gender, phone, email, description;
    
            try{
                ResultSet result = dbManager.preparedStatement.executeQuery();
                while(result.next()){                  //若有数据,就输出
                    id = result.getString(1);
                    name = result.getString(2);
                    gender = result.getString(3);
                    phone = result.getString(4);
                    email = result.getString(5);
                    description = result.getString(6);
                    //显示出每一行数据
                    System.out.println(id + "  " + name + "  " + gender + "  "
                                        + phone + "  " + email + "  " + description);
                }
                result.close();
                dbManager.close();
                
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    ···
    

    相关文章

      网友评论

          本文标题:mysql

          本文链接:https://www.haomeiwen.com/subject/hkfephtx.html