作用:创建的Statement对象执行SQL语句
(1)对象有Connection对象调用createStatement()方法创建
(2)有Statement对象调用executeUpdate()方法执行SQL语句
(3)调用的SQL语句可以是insert delete update但不可以是select
import com.mysql.jdbc.Statement;import java.sql.Connection;import java.sql.Driver;import java.sql.ResultSet;import java.util.Properties; /** * Created by I am master on 2016/9/29. */public class finallyjdbc { public static void main(String[] args) throws Exception{ Connection connection=null; Statement statement=null; //防止抛出的异常导致文件关闭失败 try { Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3306/student"; Properties properties = new Properties(); properties.put("user", "root"); properties.put("password", "root"); connection = driver.connect(url, properties); statement = (Statement) connection.createStatement(); String query = "insert into studentInfo value(‘5‘,‘小五‘,‘17‘,‘计算机科学‘)"; statement.executeUpdate(query); System.out.println("加入成功"); }catch (Exception e){ e.printStackTrace(); }finally { try { statement.close(); }catch (Exception e){ e.printStackTrace(); }finally { connection.close(); } } }}
时间: 2024-12-04 21:30:07