Java自学-JDBC 增、删、改
程序员文章站
2022-05-18 20:23:43
JDBC 增、删、改 CRUD是最常见的数据库操作,即增删改查 C 增加(Create) R 读取查询(Retrieve) U 更新(Update) D 删除(Delete) 在JDBC中增加,删除,修改的操作都很类似,只是传递不同的SQL语句就行了。 步骤 1 : 增加 这段代码就是Hello J ......
jdbc 增、删、改
crud是最常见的数据库操作,即增删改查
c 增加(create)
r 读取查询(retrieve)
u 更新(update)
d 删除(delete)
在jdbc中增加,删除,修改的操作都很类似,只是传递不同的sql语句就行了。
步骤 1 : 增加
这段代码就是hello jdbc中的代码,用于向数据库中插入数据
package jdbc; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; public class testjdbc { public static void main(string[] args) { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { e.printstacktrace(); } try ( connection c = drivermanager.getconnection("jdbc:mysql://127.0.0.1:3306/how2java?characterencoding=utf-8", "root", "admin"); statement s = c.createstatement(); ) { string sql = "insert into hero values(null," + "'提莫'" + "," + 313.0f + "," + 50 + ")"; s.execute(sql); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
步骤 2 : 删除
删除和增加很类似,只不过是执行的sql语句不一样罢了
package jdbc; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; public class testjdbc { public static void main(string[] args) { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { e.printstacktrace(); } try ( connection c = drivermanager.getconnection("jdbc:mysql://127.0.0.1:3306/how2java?characterencoding=utf-8", "root", "admin"); statement s = c.createstatement(); ) { string sql = "delete from hero where id = 5"; s.execute(sql); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
步骤 3 : 修改
修改也一样,执行另一条sql语句就可以了
package jdbc; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; public class testjdbc { public static void main(string[] args) { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { e.printstacktrace(); } try ( connection c = drivermanager.getconnection("jdbc:mysql://127.0.0.1:3306/how2java?characterencoding=utf-8", "root", "admin"); statement s = c.createstatement(); ) { string sql = "update hero set name = 'name 5' where id = 3"; s.execute(sql); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
练习:
设计一个方法
public static void execute(string sql)
方法接受的参数是sql语句,无论是增加,删除,还是修改,都调用这个方法,每次传不同的sql语句作为参数
答案 :
package jdbc; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; public class testjdbc { public static void execute(string sql) { try { class.forname("com.mysql.jdbc.driver"); } catch (classnotfoundexception e) { e.printstacktrace(); } try (connection c = drivermanager.getconnection("jdbc:mysql://127.0.0.1:3306/how2java?characterencoding=utf-8", "root", "admin"); statement s = c.createstatement();) { s.execute(sql); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } public static void main(string[] args) { execute("delete from hero where id = 1"); } }
上一篇: Spring MVC 执行流程分析
下一篇: Redis命令geoXXX