Statement概述
Statement 是 Java 執行數據庫操作的一個重要接口,用於在已經建立數據庫連接的基礎上,向數據庫發送要執行的SQL語句。Statement對象,用於執行不帶參數的簡單SQL語句。
Statement子接口有:CallableStatement, PreparedStatement。
Statement 接口提供了執行語句和獲取結果的基本方法。PreparedStatement 接口添加了處理 IN 參數的方法;而 CallableStatement 添加了處理 OUT 參數的方法。
Statement 對象用於將 SQL 語句發送到數據庫中。實際上有三種 Statement 對象,它們都作爲在給定連接上執行 SQL 語句的包容器:Statement、PreparedStatement(它從 Statement 繼承而來)和 CallableStatement(它從 PreparedStatement 繼承而來)。它們都專用於發送特定類型的 SQL 語句: Statement 對象用於執行不帶參數的簡單 SQL 語句;PreparedStatement 對象用於執行帶或不帶 IN 參數的預編譯 SQL 語句;CallableStatement 對象用於執行對數據庫已存在的存儲過程的調用。
Statement執行DDL語句
package com.JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DDLStatement {
protected String url="jdbc:mysql://localhost:3306/user";
protected String user="root";
protected String password="123456";
protected String driverName="com.mysql.jdbc.Driver";
protected Connection conn=null;
protected Statement state=null;
public void create() {
try {
Class.forName(driverName);
conn=DriverManager.getConnection(url,user,password);
if(conn!=null) {
System.out.println("數據庫連接成功!");
}
//創建statement對象
state=conn.createStatement();
//創建SQL語句
String sql="create table login(id int primary key not null auto_increment,username varchar(50),password varchar(50))";
//發送SQL語句並執行結果
int flag=state.executeUpdate(sql);
System.out.println(flag);
if(flag==0) {
System.out.println("表創建成功!");
}else {
System.out.println("表創建失敗!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
程序運行結果:
數據庫結果:
Statement執行DML語
3.1 數據庫連接代碼
package com.JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCConection3 {
private String url="jdbc:mysql://localhost:3306/user";
private String user="root";
private String password="123456";
private Connection conn=null;
private Statement state=null;
public Connection getConn() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
return conn;
}
public void close(Statement state,Connection conn) {
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.2 增加數據
package com.JDBC;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DMLInsert extends JDBCConection3{
protected Connection conn=null;
protected Statement state=null;
public void insert() throws ClassNotFoundException, SQLException {
//連接數據庫
conn=super.getConn();
//編寫SQL語句
String sql="insert into login (username,password) values('hehe','123456')";
//創建Statement對象
state=conn.createStatement();
int flag=state.executeUpdate(sql);
System.out.println(flag);
if(flag>0) {
System.out.println("數據插入成功");
}
super.close(state, conn);
}
}
程序運行結果:
數據庫結果:
3.3 更新數據
package com.JDBC;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DMLUpdate extends JDBCConection3 {
protected Connection conn=null;
protected Statement state=null;
public void update() throws ClassNotFoundException, SQLException {
//數據庫連接
conn=super.getConn();
//編寫SQL語句
String sql="update login set password='456789' where username='hehe'";
//創建Statement對象
state=conn.createStatement();
int flag=state.executeUpdate(sql);
System.out.println(flag);
if(flag>0) {
System.out.println("數據更新成功!");
}else {
System.out.println("數據更新失敗!");
}
super.close(state, conn);
}
}
程序運行結果:
數據庫結果:
3.4 刪除數據
package com.JDBC;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DMLDelete extends JDBCConection3{
protected Connection conn=null;
protected Statement state=null;
public void delete() throws ClassNotFoundException, SQLException {
//數據庫連接
conn=super.getConn();
//編寫SQL語句
String sql="delete from login where username='effort'";
//創建Statement對象
state=conn.createStatement();
int flag=state.executeUpdate(sql);
System.out.println(flag);
if(flag>0) {
System.out.println("數據刪除成功!");
}else {
System.out.println("數據刪除失敗!");
}
super.close(state, conn);
}
}
程序運行結果:
數據庫結果:
Statement執行DQL語句
4.1 查詢數據
package com.JDBC;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DQLSelect extends JDBCConection3{
protected Connection conn=null;
protected Statement state=null;
protected ResultSet rs=null;
public void selectAll() throws ClassNotFoundException, SQLException {
//數據庫連接
conn=super.getConn();
//編寫SQL語句
String sql="select * from login";
//創建Statement對象
state=conn.createStatement();
//接收查詢到的所有數據
ResultSet rs=state.executeQuery(sql);
while(rs.next()) {
System.out.println("用列名稱取值: id:"+rs.getInt("id")+" username:"+rs.getString("username")+" password:"+rs.getString("password"));
System.out.println("用索引取值: id:"+rs.getInt(1)+" username:"+rs.getString(2)+" password:"+rs.getString(3));
}
super.close(rs, state, conn);
}
}
程序運行結果:
數據庫結果: