Java Web(3)

连接数据库的步骤中Statement和PrepareStatement的区别:
Statement:
String sql="insert into student(stuo,stuname) values('"name"',"age"+")";
Statement.excuteupadte(sql);
PrepareStatement:
String sql="insert into student(stuo,stuname) values(?,?)";
PrepareStatement=connection.PrepareStatement(sql); //预编译SQL
PrepareStatement.setString(1,name);
PrepareStatement.setInt(2,age);

2.提高性能
需要重复100条数(因为有预编译,只编译一次)
Statement:
String sql="insert into student(stuo,stuname) values('"name"',"age"+")";
for(100){
Statement.excuteupadte(sql);
}
PrepareStatement:
String sql="insert into student(stuo,stuname) values(?,?)";
PrepareStatement=connection.PrepareStatement(sql); //预编译SQL
PrepareStatement.setString(1,name);
PrepareStatement.setInt(2,age);
for(100){
PrepareStatement.excuteupadte(sql);
}
3.安全方面
Statement:存在被Sql注入的风险
PrepareStatement:防止Sql注入
开发中推荐使用prepareStatement
1.jdbc总结(模板、八股文):

try{
a.导入驱动包、加载具体驱动类Class.forName("具体驱动类");
b.与数据库建立连接connection = DriverManager.getConnection(...);
c.通过connection,获取操作数据库的对象(Statement\preparedStatement\callablestatement)
stmt = connection.createStatement();
d.(查询)处理结果集rs = pstmt.executeQuery()
while(rs.next()){ rs.getXxx(..) ;}
}catch(ClassNotFoundException e )
{ ...}
catch(SQLException e)
{...
}
catch(Exception e)
{...
}
finally
{
//打开顺序,与关闭顺序相反
if(rs!=null)rs.close()
if(stmt!=null) stmt.close();
if(connection!=null)connection.close();
}

--jdbc中,除了Class.forName() 抛出ClassNotFoundException,其余方法全部抛SQLException

处理大文本数据
处理稍大型 数据:
a.存储路径
b.
CLOB:大文本数据(小说->数据)
BLOB:二进制数据

clob:大文本数据 字符流 Reader Writer

1.先通过pstmt 的? 代替小说内容 (占位符)
2.再通过pstmt.setCharacterStream(2, reader, (int)file.length()); 将上一步的?替换为 小说流, 注意第三个参数需要是 Int类型

取:
1.通过Reader reader = rs.getCharacterStream("NOVEL") ; 将cloc类型的数据 保存到Reader对象中
2. 将Reader通过Writer输出即可。

blob:二进制 字节流 InputStream OutputStream
与CLOB步骤基本一致,区别:setBinaryStream(...) getBinaryStream(...)

全部评论

相关推荐

迷茫的大四🐶:干脆大厂搞个收费培训得了,这样就人均大厂了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务