1、创建maven项目
-
下载maven包
-
修改maven/conf/setting.xml文件:添加阿里云镜像mirror,修改仓库地址localRepository
<mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror>
-
IDEA配置:指定路径 -> maven根目录 ,setting.xml , repo
- 将maven作为第三方库加入
-
根据mysql版本导入mysql驱动(百度 mvn repo找到官网,我导入的是8.0.30版本的)
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.30</version> </dependency>
2、JDBC程序入门
7步:
1.加载驱动( 如果JDBC驱动的版本高于4,那么这一步可以省略) 2.创建连接 3.准备SQL 4.编译 5.执行 6.获取结果 7.关闭资源
jdbc连接的查询
(statement会产生sql注入,所以就不演示了,直接预编译sql语句):
package com.java;
import java.sql.*;
public class Main2 {
// schema(jdbc:mysql://) | domain(localhost):port(3306) | path(/sql_store) | params
private static final String url="jdbc:mysql://localhost:3306/sql_store";
private static final String user = "root";
private static final String password ="123456";
public static void main(String[] args) throws SQLException {
boolean flag = plus("x", "'or'1'='1");
System.out.println(flag);//false
}
public static boolean plus(String firstname,String lastname) throws SQLException {
String sql ="select * from customers where first_name= ? and last_name= ?";
ResultSet rs =null;
try(
Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement ps = con.prepareStatement(sql);
){
ps.setString(1,firstname);
ps.setString(2,lastname);
rs= ps.executeQuery();
return rs.next();
}finally{
if(rs!=null) rs.close();
}
}
}
3、IDEA 快速开始maven项目(新建项目搜索quik)
Advaced Settings高级设置
- GroupId:一般可以公司的域名来设置
- ArtifactId:项目名
<!-- 上述问题可进行如下配置-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
(具体细节的操作可自行百度,会有详细的教程教你,下面是我生成的jar包并在其他项目导入)
4、导入lombok(maven仓库搜索)
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
@Data -> 注解生成getter和setter
@NoArgsConstuctor -> 无参构造
@AllArgsConstuctor ->所有构造
5、安装插件
alt+enter加入set代码
jdbc连接的修改和删除操作
import java.sql.*;
public class Main {
//url由四部分组成jdbc:mysql://(domain)localhost:(port)3306/(database)sql_store
private static final String url="jdbc:mysql://localhost:3306/sql_store";
private static final String user = "root";
private static final String password ="123456";
public static void main(String[] args) throws SQLException {
Customer customers = new Customer();
customers.setFirst_name("rod");
customers.setLast_name("json");
customers.setBirth_date(new java.util.Date());
customers.setPhone("123");
customers.setAddress("xx");
customers.setCity("xx");
customers.setState("xx");
customers.setPoints(800);
update(customers);
delete(11);
}
public static void delete(int id) throws SQLException {
String sql="delete from customers where customer_id=?";
Connection con =null;
PreparedStatement ps =null;
try {
con = DriverManager.getConnection(url, user, password);
ps = con.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
} finally {
try {
if(ps!=null&&ps.isClosed()) ps.close();
} finally {
if(con!=null&&con.isClosed()) con.close</