package demo.tt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBUtil {
/**
* @param args
* @throws Exception
* @author gaojs(QQ:525354786)
*/
public static void main(String[] args) throws Exception {
String sql = "";
String DRTVER = "";
String URL ="";
String USERNAME ="";
String PASSWORD ="";
//反射运行时候加载 JDBC到JVM中
Class.forName(DRTVER);
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
conn.setAutoCommit(false);
//方案一:
Statement stm = conn.createStatement();
//执行Update类型SQL语句
boolean flag = stm.execute(sql);
//执行Query类型SQl语句
ResultSet rs = stm.executeQuery(sql);
//方案二:
/**
可以处理带有占位符号的SQL语句,
(一、)减少SQL语句的硬解析,提高缓冲池SQL执行计划命中率
(二、)可以有效防止SQL语句注入*/
PreparedStatement pstm = conn.prepareStatement(sql);
//执行Update类型SQL语句
boolean flag1 = pstm.execute();
//执行Query类型SQl语句
ResultSet rs1 =pstm.executeQuery();
//给占位符号赋值
//pstm.setObject(parameterIndex, x);
//无异常commit,反之rollback
conn.commit();
conn.rollback();
//处理结果集
while(rs.next()){
}
//关闭连接,释放资源
//rs.close();
//pstm.close();
//stm.close();
//conn.close();
}
}