Environment introduction:
Editor: Eclipse
mysql database jar package: mysql-connector-java-8.0.22.jar
java version: 1.8*
How to load eclipse jar package, refer to https://jingyan.baidu.com/article/f3e34a127f837db5eb6535d3.html here is not the main focus
main content:
The main method code is as follows:
public static void main(String[] args) {
// 声明Connection对象
Connection con = null;
// 驱动程序名
// String driver = "com.mysql.jdbc.Driver"; // 可能会提示错误信息 但是不影响
String driver = "com.mysql.cj.jdbc.Driver"; // MySql 6版本使用
// 数据库链接 ip:port 其中test为数据库
String url = "jdbc:mysql://105.105.105.105:3306/test";
// 用户名 自行修改
String user = "***";
// 密码 自行修改
String password = "********";
// 目标数据表 自行修改
String table = "*****";
System.out.println("数据查询");
query_sql(con,driver,url,user,password,table);
System.out.println("数据插入");
add_sql(con,driver,url,user,password,table);
System.out.println("数据删除");
del_sql(con,driver,url,user,password,table);
System.out.println("数据更改");
update_sql(con,driver,url,user,password,table);
}
One: Increase of data (insert)
// 增
public static void add_sql(Connection con,String driver,String url,String user,String password,String table){
String min_ip = "210.69.0.1 ";
String max_ip = "210.69.255.224";
try {
Class.forName(driver);
// 链接MySql
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed()) {
System.out.println("数据库连接成功");
};
String insert_sql = "insert into " + table + " (minip,maxip) values(?,?)";
PreparedStatement ps = con.prepareStatement(insert_sql);
// 注意形参?的下标从1开始
ps.setString(1, min_ip);
ps.setString(2, max_ip);
int num = ps.executeUpdate();
if(num == 1) {
System.out.println("数据插入成功");
}else {
System.out.println("数据插入失败");
}
ps.close();
con.close();
}catch(SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Two: the deletion of data (delete)
// 删
public static void del_sql(Connection con,String driver, String url,String user,String password,String table) {
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
if (!con.isClosed()) {
System.out.println("数据库连接成功!!!");
}
String del_sqlString = "delete from " + table + " where id = ? ;";
PreparedStatement ps = con.prepareStatement(del_sqlString);
// 删除id为73837的数据记录
ps.setInt(1, 73837);
int num = ps.executeUpdate();
System.out.println(num);
if (num == 1) {
System.out.println("数据删除成功");
}else {
System.out.println("数据删除失败");
}
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Three: data changes (update)
// 改
public static void update_sql(Connection con,String driver,String url,String user,String password,String table) {
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,password);
if (!con.isClosed()) {
System.out.println("数据库连接成功");
}
String up_sql = "update " + table + " set minip = ? where id = ?;";
PreparedStatement ps = con.prepareStatement(up_sql);
// 将id为73838的数据记录的minip更新为"8888888"
ps.setString(1, "8888888");
ps.setInt(2, 73838);
int num = ps.executeUpdate();
if (num == 1) {
System.out.println("数据更新成功");
}else {
System.out.println("数据更新失败");
}
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Four: data query (query)
// 查
public static void query_sql(Connection con, String driver, String url, String user, String password,String table) {
try {
// 加载驱动程序
Class.forName(driver);
// 连接MySql
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed()) {
System.out.println("数据库连接成功");
};
// 创建statement类对象 用来执行SQL语句
Statement statement = con.createStatement();
String sql = "select minip,maxip from " + table + " limit 10 ";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
String minip = rs.getString("minip"); // 字段名minip
String maxip = rs.getString("maxip"); // 字段名maxip
System.out.println(minip + " ---- " + maxip);
}
System.out.println("关闭数据库连接");
con.close(); //关闭数据库
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
Summary : The above is a simple operation of adding, deleting, modifying and checking when java connects to mysql database
Remarks: The above-mentioned one main method and 4 non-main methods can be placed in the same class for calling. I have tested it. Available