Java连接MySQL实现数据的增删改查附实际代码环境介绍:
编辑器:Eclipse
mysql数据库jar包:mysql-connector-java-8.0.22.jar
java版本:1.8*
主要内容:
主⽅法代码如下所⽰:
public static void main(String[] args) {
//  声明Connection对象
停止你的内在战争Connection con = null;
// 驱动程序名
java课程设计小游戏有哪些//  String driver = "sql.jdbc.Driver"; //  可能会提⽰错误信息但是不影响
String driver = "sql.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)
String min_ip = "210.69.0.1 ";
String max_ip = "210.69.255.224";
try {
Class.forName(driver);
// 链接MySql
con = Connection(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 = Connection(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("数据删除失败");
}
mysql菜鸟教程增删改查
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
三:数据的更改(update)
Class.forName(driver);
con = Connection(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();sql2000数据库修复命令
ascii码与十进制转换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 = Connection(url,user,password);
if(!con.isClosed()) {
System.out.println("数据库连接成功");
};
// 创建statement类对象⽤来执⾏SQL语句
Statement statement = ateStatement();
String sql = "select minip,maxip from " + table + " limit 10 ";
ResultSet rs = uteQuery(sql);
()) {
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个⾮主⽅法,可放置在同⼀个类中进⾏调⽤,本⼈已经测试过 可⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。