Mysql+使用Statement和PreparedStatement對象執行sql語句+java

1.PreparedStatement

package servletDemo;
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 JDBCPreparedStatement {
	private static final String URL = "jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC";
	private static final String USERNAME = "root";
	private static final String PWD = "root";
	public static void update(){//增刪改
		Connection connection = null;//因爲有可能還麼有用到connection就報錯了 那麼這個對象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		try{
			//1導入驅動,加載具體的驅動類
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2與DB建立連接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3發送sql語句,執行(增刪改)
			//pstmt需要預編譯sql
			int stunumber = 3;
			String sname = "WangWu";
			String sql = "insert into student values (?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, stunumber);
			pstmt.setString(2,sname);
//			String sql = "insert into student values (1,'zs',23,'s1')";
			//String sql = "update student set name = 'tian' where number = 1";
			//String sql = "delete from student where number = 1";
			//執行SQL
			
			int count = pstmt.executeUpdate();//返回表示增刪改 了幾條數據
			//4處理結果集
			if(count>0){
				System.out.println("操作成功!");
			}
		}catch (ClassNotFoundException e) {//Class.forName()的異常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()異常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是無論有沒有異常都需要關閉連接
			try{
				if(pstmt!=null){//執行sql語句
					pstmt.close();
				}
				if(connection!=null){//建立java代碼和數據庫的連接
					connection.close();
				}
			}catch(SQLException e) {//前面兩個close 都會拋SQLException異常
				e.printStackTrace();
			}
		}		
	}
	public static void query(){//查
		Connection connection = null;//因爲有可能還麼有用到connection就報錯了 那麼這個對象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		ResultSet rs = null;
		try{
			//1導入驅動,加載具體的驅動類
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2與DB建立連接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3發送sql語句,執行(增刪改)
			String name = "%W%";
			String sql = "select * from student where stuname like ?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, name);
//			pstmt.setString(2, "stuname");
//			pstmt.setString(3, "China");
//			
			//執行SQL
			rs = pstmt.executeQuery();//返回表示增刪改 了幾條數據
			//4處理結果集
			//rs指向結果集的前一個元素 結果集相當於一張表
			while(rs.next()){//判斷rs指向的下一個元素有沒有數據
				int sno = rs.getInt("stuno");
				//int sno = rs.getInt(1);
				String sname = rs.getString("stuname");//裏面寫列名
				//也可以寫序號 一行中的第幾列 但是不推薦
				//String sname = rs.getString(2);
				System.out.println(sno + "--"+sname);
			}
		}catch (ClassNotFoundException e) {//Class.forName()的異常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()異常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是無論有沒有異常都需要關閉連接
			try{
				if(pstmt!=null){//執行sql語句
					pstmt.close();
				}
				if(connection!=null){//建立java代碼和數據庫的連接
					connection.close();
				}
			}catch(SQLException e) {//前面兩個close 都會拋SQLException異常
				e.printStackTrace();
			}
		}		
	}
	
	public static void main(String [] args){
		update();
//		query();
	}
}






2.Statement

package servletDemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCStatement {
	private static final String URL = "jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC";
	private static final String USERNAME = "root";
	private static final String PWD = "root";
	public static void update(){//增刪改
		Connection connection = null;//因爲有可能還麼有用到connection就報錯了 那麼這個對象是空的就不能使用close方法
		Statement stmt = null;//同理
		try{
			//1導入驅動,加載具體的驅動類
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2與DB建立連接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3發送sql語句,執行(增刪改)
			stmt = connection.createStatement();
			String sql = "insert into student values (6,'Wanmei')";
//			String sql = "update student set stuname = 'tian' where stuno = 1";
//			String sql = "delete from student where stuno = 7";
			//執行SQL
			int count = stmt.executeUpdate(sql);//返回表示增刪改 了幾條數據
			//4處理結果集
			if(count>0){
				System.out.println("操作成功!");
			}
		}catch (ClassNotFoundException e) {//Class.forName()的異常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()異常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是無論有沒有異常都需要關閉連接
			try{
				if(stmt!=null){//執行sql語句
					stmt.close();
				}
				if(connection!=null){//建立java代碼和數據庫的連接
					connection.close();
				}
			}catch(SQLException e) {//前面兩個close 都會拋SQLException異常
				e.printStackTrace();
			}
		}		
	}
	public static void query(){//查
		Connection connection = null;//因爲有可能還麼有用到connection就報錯了 那麼這個對象是空的就不能使用close方法
		Statement stmt = null;//同理
		ResultSet rs = null;
		try{
			//1導入驅動,加載具體的驅動類
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2與DB建立連接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3發送sql語句,執行(增刪改)
			stmt = connection.createStatement();
			String sql = "select stuno,stuname from student";
			//執行SQL
			rs = stmt.executeQuery(sql);//返回表示增刪改 了幾條數據
			//4處理結果集
			//rs指向結果集的前一個元素 結果集相當於一張表
			while(rs.next()){//判斷rs指向的下一個元素有沒有數據
				int sno = rs.getInt("stuno");
				//int sno = rs.getInt(1);
				String sname = rs.getString("stuname");//裏面寫列名
				//也可以寫序號 一行中的第幾列 但是不推薦
				//String sname = rs.getString(2);
				System.out.println(sno + "--"+sname);
			}
		}catch (ClassNotFoundException e) {//Class.forName()的異常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()異常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是無論有沒有異常都需要關閉連接
			try{
				if(stmt!=null){//執行sql語句
					stmt.close();
				}
				if(connection!=null){//建立java代碼和數據庫的連接
					connection.close();
				}
			}catch(SQLException e) {//前面兩個close 都會拋SQLException異常
				e.printStackTrace();
			}
		}		
	}
	
	public static void main(String [] args){
		update();
		query();
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章