jdbc Statement和PrepareStatement操作
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnHandle {
private static Connection conn=null;
private static Statement stmt=null;
private static PreparedStatement pstmt=null;
private static CallableStatement callstmt=null;
private static ResultSet rs=null;
//獲取資料庫連線
public static Connection getConn() {
String url="jdbc:mysql://127.0.0.1/dandan?user=root&password=dada&characterEncoding=utf-8";
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//執行方法
public static void main(String[] args) throws Exception {
//載入驅動
Class.forName("org.gjt.mm.mysql.Driver");
//獲取Connection
conn=ConnHandle.getConn();
/*******************************Statement handle**********************************************/
//獲取stmt
// stmt=conn.createStatement();
//normal insert
// String normal_insert="insert into student(name,sex,grade) values('dada','m','100000')";
// stmt.executeUpdate(normal_insert);
//normal delete
// String normal_delete="delete from student where name='dada'";
// stmt.executeUpdate(normal_delete);
//normal update
// String normal_update="update student set name='dada' where name in('hehe','tongtong')";
// stmt.executeUpdate(normal_update);
//normal query
// String view_sql="select * from student";
// rs=stmt.executeQuery(view_sql);
// ConnHandle.show(rs);
/*******************************Statement handle**********************************************/
//正常的資料庫操作是裡面所執行的方法是要傳遞引數的,因為它無法事先的建立有引數的查詢物件
//而Preparestatement則可以,所以它可以直接呼叫executeUpdate()裡面不用傳遞引數,因為上面
//已經把引數傳遞好了
/*******************************PrepareStatement handle**********************************************/
//PrepareStatement insert
// String pre_insert="insert into student(name,sex,grade) values (?,?,?)";
// pstmt=conn.prepareStatement(pre_insert);
// pstmt.setString(1, "dada1");
// pstmt.setString(2, "m");
// pstmt.setString(3, "1");
// pstmt.executeUpdate();
// //PrepareStatement delete
// String pre_delete="delete from student where name=?";
// pstmt=conn.prepareStatement(pre_delete);
// pstmt.setString(1, "dada1");
// pstmt.executeUpdate();
//PrepareStatement update
String pre_update="update student set name=? where name=?";
pstmt=conn.prepareStatement(pre_update);
pstmt.setString(1, "jack");
pstmt.setString(2, "dada");
pstmt.executeUpdate();
//PrepareStatement query
String pre_query="select * from student";
pstmt=conn.prepareStatement(pre_query);
rs=pstmt.executeQuery();
ConnHandle.show(rs);
ConnHandle.closeAll();
/*******************************PrepareStatement handle**********************************************/
}
public int normal_update(String insert) {
try {
result = stmt.executeUpdate(insert);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//normal query
public ResultSet normal_query(String query) {
try {
rs=stmt.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
public static void show(ResultSet rs) {
try {
while(rs.next()) {
System.out.println("Row: "+rs.getRow()+"\tId: "+rs.getInt(1)+"\tName: "+rs.getString(2)
+"\tSex: "+rs.getString(3)+"\tGrade: "+rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//把關閉所有連線,寫成方法直接呼叫
public static void closeAll() throws Exception {
if(null!=conn) {
if(rs!=null) {
rs.close();
}
if(stmt!=null) {
stmt.close();
}
if(pstmt!=null) {
pstmt.close();
}
if(callstmt!=null) {
callstmt.close();
}
conn.close();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnHandle {
private static Connection conn=null;
private static Statement stmt=null;
private static PreparedStatement pstmt=null;
private static CallableStatement callstmt=null;
private static ResultSet rs=null;
//獲取資料庫連線
public static Connection getConn() {
String url="jdbc:mysql://127.0.0.1/dandan?user=root&password=dada&characterEncoding=utf-8";
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//執行方法
public static void main(String[] args) throws Exception {
//載入驅動
Class.forName("org.gjt.mm.mysql.Driver");
//獲取Connection
conn=ConnHandle.getConn();
/*******************************Statement handle**********************************************/
//獲取stmt
// stmt=conn.createStatement();
//normal insert
// String normal_insert="insert into student(name,sex,grade) values('dada','m','100000')";
// stmt.executeUpdate(normal_insert);
//normal delete
// String normal_delete="delete from student where name='dada'";
// stmt.executeUpdate(normal_delete);
//normal update
// String normal_update="update student set name='dada' where name in('hehe','tongtong')";
// stmt.executeUpdate(normal_update);
//normal query
// String view_sql="select * from student";
// rs=stmt.executeQuery(view_sql);
// ConnHandle.show(rs);
/*******************************Statement handle**********************************************/
//正常的資料庫操作是裡面所執行的方法是要傳遞引數的,因為它無法事先的建立有引數的查詢物件
//而Preparestatement則可以,所以它可以直接呼叫executeUpdate()裡面不用傳遞引數,因為上面
//已經把引數傳遞好了
/*******************************PrepareStatement handle**********************************************/
//PrepareStatement insert
// String pre_insert="insert into student(name,sex,grade) values (?,?,?)";
// pstmt=conn.prepareStatement(pre_insert);
// pstmt.setString(1, "dada1");
// pstmt.setString(2, "m");
// pstmt.setString(3, "1");
// pstmt.executeUpdate();
// //PrepareStatement delete
// String pre_delete="delete from student where name=?";
// pstmt=conn.prepareStatement(pre_delete);
// pstmt.setString(1, "dada1");
// pstmt.executeUpdate();
//PrepareStatement update
String pre_update="update student set name=? where name=?";
pstmt=conn.prepareStatement(pre_update);
pstmt.setString(1, "jack");
pstmt.setString(2, "dada");
pstmt.executeUpdate();
//PrepareStatement query
String pre_query="select * from student";
pstmt=conn.prepareStatement(pre_query);
rs=pstmt.executeQuery();
ConnHandle.show(rs);
ConnHandle.closeAll();
/*******************************PrepareStatement handle**********************************************/
}
public int normal_update(String insert) {
try {
result = stmt.executeUpdate(insert);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//normal query
public ResultSet normal_query(String query) {
try {
rs=stmt.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void show(ResultSet rs) {
try {
while(rs.next()) {
System.out.println("Row: "+rs.getRow()+"\tId: "+rs.getInt(1)+"\tName: "+rs.getString(2)
+"\tSex: "+rs.getString(3)+"\tGrade: "+rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//PrepareStatement date1 把傳遞過來的util型別的Date轉換為sql型別的date
public static void setDate(int index,Date date) throws SQLException{
pstmt.setDate(index, new java.sql.Date(date.getTime()));
}
//PrepareStatement date2 把String引數首先轉換為Date然後再轉換為sql型別Date
public static void setDate(int index,String pattern,String string) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(string);
pstmt.setDate(index, new java.sql.Date(date.getTime()));
}
//把關閉所有連線,寫成方法直接呼叫
public static void closeAll() throws Exception {
if(null!=conn) {
if(rs!=null) {
rs.close();
}
if(stmt!=null) {
stmt.close();
}
if(pstmt!=null) {
pstmt.close();
}
if(callstmt!=null) {
callstmt.close();
}
conn.close();
}
}
}
相關文章
- statement 、prepareStatement的用法和解釋REST
- PrepareStatement與Statement之間的區別REST
- JDBC - Statement物件 - executeBatch()和executeUpdate()JDBC物件BAT
- prepareStatement和Statement執行批處理的執行情況REST
- JDBC入門(一):Statement物件JDBC物件
- 使用PrepareStatement實現批量插入操作REST
- JDBC之Statement,PreparedStatement,CallableStatement的區別JDBC
- jdbc connection是否可以建立多個statementJDBC
- Statement (操作 SQL 語句)SQL
- JDBC TM入門指南6--Parepared Statement (轉)JDBC
- jdbc操作AutoCommitJDBCMIT
- 聊聊jdbc的batch操作JDBCBAT
- Mysql異常刨析:Could not commit JDBC transaction;No operations allowed after statement closedMySqlMITJDBC
- Statement
- Statement和PreparedStatement之間的區別
- 【Mybatis原始碼解析】- JDBC連線資料庫的原理和操作MyBatis原始碼JDBC資料庫
- JDBC-MySql基礎操作詳解JDBCMySql
- JDBC 2.0和4.0JDBC
- Spring04——Spring操作JdbcTemplate進行JDBC操作SpringJDBC
- JDBC3——SQL隱碼攻擊、及其解決方法——Statement與PreparedStatement對比——PreparedStatement的CRUDJDBCSQL
- 練習最原始的JDBC的基本操作JDBC
- JS - if else and else if statementJS
- JDBC中的executeQuery和executeUpdateJDBC
- 通過 Spring 框架如何進行JDBC操作呢?Spring框架JDBC
- 對於JDBC資料庫的初始化操作JDBC資料庫
- GCC編譯遇到“a label can only be part of a statement and a declaration is not a statement”問題GC編譯
- 不用JDBC:ODBC bridge直接操作Access 資料庫 (轉)JDBC資料庫
- 安全性、JDBC和其他方面JDBC
- Understanding the CREATE DATABASE Statement (69)Database
- 使用JDBC操作SAP雲平臺上的HANA資料庫JDBC資料庫
- MySQL:You must reset your password using ALTER USER statement before executing this statement.MySql
- PrepareStatement物件進行批處理的典型步驟順序REST物件
- 深入淺出MyBatis:JDBC和MyBatis介紹MyBatisJDBC
- 用JDBC操縱BLOB和CLOB資料JDBC
- mycat和sharding JDBC分庫分表JDBC
- 求救:jdbc與mysql透過Servlet操作後連線關閉不了JDBCMySqlServlet
- Oracle vs PostgreSQL Develop(16) - Prepared StatementOracleSQLdev
- 追溯 MySQL Statement Cancellation TimerMySql