项目依赖和配置
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
mybatis
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
junit单元测试
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
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();
<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>

浙公网安备 33010602011771号