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#laterdriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&useSSL=false username: rootpassword: 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:定义sqlString 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.执行sqlResultSet resultSet = pstmt.executeQuery();//判断是否成功与否if(resultSet.next()){System.out.println("登录成功!");}else{System.out.println("登录失败");}//7.释放资源 conn.close();pstmt.close();resultSet.close();