您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
08_JDBC练习_insert语句
发布时间:2022-07-17 19:35:33编辑:雪饮阅读()
package day5;
import java.sql.*;
public class JdbcTest {
public static void main(String[] args) {
//继上篇,本篇将会了解下如何使用finally关键语句来jdbc相关的使用后的内存占用的回收
Connection conn=null;
Statement stmt=null;
try {
conn= DriverManager.getConnection("jdbc:mysql:///test","root","root");
//定义要执行的sql
String sql="insert into t4 values(null,'kasumi')";
//执行sql
//这样直接执行,则Statement就是匿名的,这样的情况下是否state会自动释放?(值得思考,哥这里认为匿名对象用一次就会被回收,所以这里这样写就可以避免还要多释放下Statement)
// int effect_rows=conn.createStatement().executeUpdate(sql);
stmt=conn.createStatement();
int effect_rows=stmt.executeUpdate(sql);
System.out.println(effect_rows);
}
catch (Exception e){
e.printStackTrace();
}
finally {
if(conn!=null){
try{
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
if(stmt!=null){
try{
stmt.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
}
关键字词:jdbc,insert,finally,释放,内存,回收