01 May 2016

insert update delete select store procedure mysql in java swing


Create Update store procedure mysql


CREATE PROCEDURE updateStudent      
 (        
     IN  p_id                   INT(11) ,         
     IN  Name                 VARCHAR(45)  ,       
     IN  Class                 VARCHAR(45)  ,       
     IN  Address              VARCHAR(145)
) 
BEGIN   
UPDATE students  
    SET       
            name = Name   
            class = Class  
            address = Address             
    WHERE id = p_id ;  
END 
http://www.sqlinfo.net/mysql/mysql_stored_procedure_UPDATE.php
String sql = "{CALL updateStudent (?,?,?,?)}";
cstmt = conn.prepareCall(sql);
     
cstmt.setInt(1, id);
cstmt.setString(2, txtName.getText());
cstmt.setString(3, txtClass.getText());
cstmt.setString(4, txtAddress.getText());
cstmt.executeUpdate();
Create query delete store procedure mysql

CREATE PROCEDURE deleteStudent (IN p_id int(11))
BEGIN
     Delete From students where id = p_id;

END
http://www.sqlinfo.net/mysql/mysql_stored_procedure_DELETE.php
String sql = "{CALL deleteStudent(?)}";
cstmt = conn.prepareCall(sql);
     
cstmt.setInt(1, id);
cstmt.executeUpdate();
Create query select store procedure mysql

CREATE PROCEDURE selectStudent()
BEGIN
     SELECT * FROM students;

END
http://www.sqlinfo.net/mysql/mysql_stored_procedure_SELECT.php
String sql = "{CALL selectStudent()}";
cstmt = conn.prepareCall(sql);
     
cstmt.executeQuery();
while(rs.next()){
 //Do some thing
}
Create query Insert store procedure mysql


CREATE PROCEDURE insertStudent(
IN p_name VARCHAR(45),
IN p_age INT(11), 
IN p_class VARCHAR(45),
IN p_address VARCHAR(145),
IN p_birthday DATE, 
IN p_images BLOB)
BEGIN
INSERT INTO students (name,age,class,address,birthday,images)

VALUES (p_name,p_age,p_class,p_address,p_birthday,p_images);

END
String sql = "{CALL insertStudent(?,?,?,?,?,?)}";
cstmt = conn.prepareCall(sql);

cstmt.setString(1, txtName.getText()); //String
cstmt.setInt(2, Integer.parseInt(txtAge.getText())); //int
cstmt.setString(3, txtClass.getText());  //String
cstmt.setString(4, txtAddress.getText());  //String
cstmt.setString(5, new SimpleDateFormat("yyyy-MM-dd").format(date)); //date
cstmt.setBinaryStream(6, fin, (int) imgfile.length()); //images

cstmt.executeUpdate();


SELECT Count OUT

CREATE PROCEDURE `getSinhVienTuoi`(OUT young int, OUT old int)
BEGIN
-- young --
    SELECT count(*) INTO young from sinhvien WHERE age < 18;
    
    -- old -- 
    SELECT count(*) INTO old from sinhvien WHERE age >= 18;
END

-- RUN in mysql --

SELECT * from sinhvien;
Call getSinhVienTuoi(@young, @old);
SELECT @old;




0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang