环境介绍:
编辑器:Eclipse
mysql数据库jar包:mysql-connector-java-8.0.22.jar
java版本:1.8*
eclipse如何加载jar包,可参考 https://jingyan.baidu.com/article/f3e34a127f837db5eb6535d3.html 这里不做重点分享
主要内容:
主方法代码如下所示:
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);
}
一:数据的增加 (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();
}
}
二:数据的删除(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();
}
}
三:数据的更改(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();
}
}
四:数据的查询(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();
}
}
总结:以上为java连接mysql数据库时的增删改查简单操作
备注:上述一个主方法 4个非主方法,可放置在同一个类中进行调用,本人已经测试过 可用