首页 > 代码库 > JDBCl链接中Statement
JDBCl链接中Statement
作用:创建的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();
}
}
}
}
JDBCl链接中Statement
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。