JDBC连接数据库的步骤以及url中“?”"&"的作用

来源:互联网 发布:伊塞亚托马斯数据 编辑:程序博客网 时间:2024/05/17 06:17

连接mysql的步骤

以一个登录行为为例。

    public void login(String username,String password) throws SQLException  {        //注册驱动        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        //获取链接        Connection          conn=DriverManager.getConnection("jdbc:mysql://loacalhost:33060/mysql","root","awsed2zx");        //编写sql语句        String sql ="select*form table where username=?,password=?";        //创建预处理对象        PreparedStatement pstmt =conn.prepareCall(sql);        //设置参数(给占位符)        pstmt.setString(1,username);        pstmt.setString(2,password);        //执行查询操作        ResultSet rs=pstmt.executeQuery();        if(rs.next()){            System.out.println("恭喜你"+username+",登录成功");            System.out.println(sql);        }else{            System.out.println("账号或密码错误");        }        if(rs!=null) rs.close();        if(pstmt!=null) pstmt.close();        if(conn!=null) conn.close();

第二个是jdbc:mysql://loacalhost:33060/mysql这个url中“?”“&”的用法
jdbc:mysql://localhost:3306/mysql?useSSL=false&useUnicode=true&characterEncoding=utf8
用?在后边可以添加属性,”&”可以连接不同的属性。