3、Statement陈述接口的子接口提前准备sql语句PreparedStatement中的方法来执行SQL语句
来源:互联网 发布:手机验钞机软件新版 编辑:程序博客网 时间:2024/05/22 00:51
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
publicclassTestPrepStatement{
publicstaticvoidmain(String[]args){
if(args.length!=2){
System.out.println("Parameter Error ! Please Input Again!");
System.exit(-1);
}
Stringname=args[0];
intage=0;
try{
age=Integer.parseInt(args[1]);
}catch(NumberFormatExceptione){
System.out.println("Parameter Error ! Age should be Number Format!");
System.exit(-1);
}
PreparedStatementpstmt=null;
Connectionconn=null;
// 跟数据库连接先找到数据库管家DriverManager(连接哪种数据库)
// new出来mysql驱动类-class.forname
try{
Class.forName("com.mysql.jdbc.Driver");
// 此时会自动注册DriverManager,可以拿到跟数据库连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/tab_demo?useSSL=false","root","123456");
// 现在已经连接到数据库上了,现在执行sql语句
// 创建一个语句对象Statement陈述语句的意思
Stringsql="INSERT INTO `tab_demo`.`inf` (`name`, `age`) VALUES (?,?)";
pstmt=conn.prepareStatement(sql);
//准备好的sql执行语句生成,下面给占位符赋值
pstmt.setString(1,name);
pstmt.setInt(2,age);
intinsert=pstmt.executeUpdate();//执行数据库添加数据操作
System.out.println(insert);
}catch(ClassNotFoundExceptione){
// 驱动找不到报异常
e.printStackTrace();
}catch(SQLExceptione){
// 后面的SQL执行异常,通常记录在log文件里,用log for J开源框架记录在文件里
e.printStackTrace();
}finally{// 关闭资源
try{
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLExceptione){
e.printStackTrace();
} } }}
0 0
- 3、Statement陈述接口的子接口提前准备sql语句PreparedStatement中的方法来执行SQL语句
- java.sql.Statement及子接口: PreparedStatement:
- java基础巩固---jdbc接口PreparedStatement执行sql语句
- JDBCTM中Statement接口提供的execute、executeQuery和executeUpdate之间的区别 Statement 接口提供了三种执行 SQL 语句的方法:
- JDBC中Statement 接口提供了三种执行 SQL 语句的方法: executeQuery executeUpdate execute 使用哪一个方法由 SQL 语句所产生的内容决定。
- Java中使用Statement接口SQL语句
- PreparedStatement 与 Statement 的区别 —— 理解动态SQL语句的执行机制
- PreparedStatement显示执行的SQL语句
- Statement三种执行SQL语句的方法
- SQL语句(statement)预处理(preparedStatement)
- 使用PreparedStatement执行sql语句
- PreparedStatement与Statement对比、sql语句的被注入问题
- JDBC中输出PreparedStatement对象中的SQL语句的方法
- Statement、PreparedStatement执行SQL
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- 使用Statement执行sql语句
- Statement接口以及Java相关SQL语句操作
- 判别模型和生成模型
- android之ContentProvider
- jdbc链接5.7版本mysql的ssl问题
- 关于岭回归
- thinkPHP返回错误:无法加载模块Http
- 3、Statement陈述接口的子接口提前准备sql语句PreparedStatement中的方法来执行SQL语句
- POJ 3785 The Next Permutation
- 学生管理系统
- 单例模式
- Java web项目 爱分享社区管理员登陆部分代码
- 数据库的多表连接查询
- PAT-B 1007. 素数对猜想 (20)
- static 在循环中的作用
- Android音乐播放器---实现Notification控制音乐播放