1. 程式人生 > >資料庫查詢的幾種方法

資料庫查詢的幾種方法

1、Statement介面查詢
這通常是比較常用的。

Statement statement = connection.createStatement();
ResultSet rs 
= getResultSet("SELECT * FROM TB_STUDENTS WHERE AGE=20");
...
while (rs.next()) 
      

       ...
      }

2、PreparedStatement介面查詢
如果需要多次執行SQL語句,那麼PreparedStatement是首選。因為他包含了一個已經被編譯的SQL語句,提高了程式的效率和效能。
下面分別對TB_CUSTOMERS表,進行查詢、插入、修改、刪除的例子。
查詢:

publicvoid preQueryCustomersTB(int ID)
        
{
            
try
                
{
                    PreparedStatement pstmt 
= connection.prepareStatement("SELECT * FROM TB_CUSTOMERS WHERE ID = ?;");
                    pstmt.setInt(
1, ID);
                    pstmt.executeQuery();
                    connection.commit();
                    pstmt.clearParameters();
                    
                }
catch (SQLException e)
                
{
                    e.printStackTrace();
                }

        }

插入:

publicboolean preInsertCustomersTB(String company,String name,String address,String email,String phone,String other)
        
{
            
try
                
{
                    PreparedStatement pstmt 
= connection.prepareStatement("INSERT INTO TB_CUSTOMERS (COMPANY,NAME,ADDRESS,EMAIL,PHONE,OTHER) VALUES (?,?,?,?,?,?) ;");
                    pstmt.setString(
1, company);
                    pstmt.setString(
2, name);
                    pstmt.setString(
3, address);
                    pstmt.setString(
4, email);
                    pstmt.setString(
5, phone);
                    pstmt.setString(
6, other);
                    pstmt.execute();
                    connection.commit();
                    
                }
catch (SQLException e)
                
{
                    
// TODO 自動生成 catch 塊
                    e.printStackTrace();
                    
returnfalse;
                }

            
returntrue;
        }

修改:

publicboolean preUpdataCustomersTB(int id,String company,String name,String address,String email,String phone,String other)
        
{
            
try
                
{
                    PreparedStatement pstmt 
= connection.prepareStatement(
                            
"update TB_CUSTOMERS SET company = ?, name = ?, address = ?, email = ?, phone = ?, other = ? WHERE ID = ?;");
                    pstmt.setString(
1, company);
                    pstmt.setString(
2, name);
                    pstmt.setString(
3, address);
                    pstmt.setString(
4, email);
                    pstmt.setString(
5, phone);
                    pstmt.setString(
6, other);
                    pstmt.setInt(
7, id);
                    pstmt.execute();
                    connection.commit();
                    
                }
catch (SQLException e)
                
{
                    
// TODO 自動生成 catch 塊
                    e.printStackTrace();
                    
returnfalse;
                }

            
returntrue;
        }

刪除:

publicvoid preDeletCustomersTB(int RowID)
        
{
            
try
                
{
                    PreparedStatement pstmt 
= connection.prepareStatement("delete FROM TB_CUSTOMERS where ID = ?;");
                    pstmt.setInt(
1, RowID);
                    pstmt.execute();
                    connection.commit();
                    
                }
catch (SQLException e)
                
{
                    
// TODO 自動生成 catch 塊
                    e.printStackTrace();
                }

        }

3、CallableStatement介面查詢 
CallableStatement介面提供了通過JDBC API呼叫SQL儲存過程的標準途徑。
使用IN引數的例子:

publicvoid callSetPlayer(String player)
    
{
        
try
            
{
                CallableStatement cStatement 
= connection.prepareCall("{CALL setPlayName(?}");
                cStatement.setString(
1"John Doe");
                cStatement.execute();
            }
catch (SQLException e)
            
{
                e.printStackTrace();
            }

    }
使用OUT引數的例子:
public String callGetPlayer()
        
{
            
try
                
{
                    CallableStatement cStatement 
= connection.prepareCall("{CALL getPlayName(?}");
                    cStatement.registerOutParameter(
1, java.sql.Types.VARCHAR);
                    cStatement.execute();
                    String name 
= cStatement.getString(1);
                    
//...
return name;
                }
catch (SQLException e)
                
{
                    e.printStackTrace();
                    
returnnull;
                }

        }