项目依赖和配置

mysql    

1、添加相关依赖
<!-- 增加相关依赖包 -->
 
        <!-- Postgresql驱动包 -->
        <!-- <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency> -->
 
        <!-- MySQL驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>



2、修改配置文件(一般是application.yml文件)
  datasource:
    #befor
      #driver-class-name: org.postgresql.Driver
      #url: jdbc:postgresql://localhost:5432/demo?useSSL=false
      #username: postgres
      #password: 12345688
    #later
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&useSSL=false
      username: root
      password: 12345688
View Code

 mybatis

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
View Code

 junit单元测试

<!-- junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
View Code

jdbc

       //1:注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2:获取连接
        String url ="jbdc:mysql:///dd1?useSSL=false";
        String username1 ="root";
        String password1 ="1234";
        Connection conn = DriverManager.getConnection(url,username1,password1);
        //定义接收用户输入和用户名和密码:
        String name ="zhangsan";
        String pwd ="ffff";
        //3:定义sql
        String sql ="select *from tb_user where username=? and password=?";
       //4.获取PreparedStatement对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //设置?
        pstmt.setString(1,name);
        pstmt.setString(2,pwd);
        //5.执行sql
        ResultSet resultSet = pstmt.executeQuery();
        //判断是否成功与否
        if(resultSet.next()){
            System.out.println("登录成功!");
        }else{
            System.out.println("登录失败");
        }
        //7.释放资源
        conn.close();
        pstmt.close();
        resultSet.close();
View Code

 

<dependencies>
        <!-- MySQL驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
    </dependencies>

 

posted @ 2024-09-06 13:38  爱哭的小孩有糖吃  阅读(24)  评论(0)    收藏  举报