一丨Statement
1.1 PerparedStatement (准备Statement,解决参数类型问题)
public static PreparedStatement getPreparedStatement(Connection conn,String sql){
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
System.err.println("*Faild In CreateStatement By Connection");
e.printStackTrace();
}
return pstmt;
}
package com.qsuron.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.qsuron.util.DB;
public class Test2 {
public static void main(String[] args) throws SQLException {
Connection conn = DB.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:xxxx/qsuron","qsuron","qsuron");
PreparedStatement pstmt = DB.getPreparedStatement(conn,"insert into student values( , , );");
pstmt.setInt(1,1213400129);
pstmt.setString(2,"123456");
pstmt.setString(3,"qsuron");
pstmt.executeUpdate();
DB.close();
}
}
1.2 CallableStatement (存储过程)
创建一个存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `p`(IN `id1` int,IN `id2` int,IN `password` char(20),IN `name` varchar(15),OUT `temp` int) BEGIN #插入id较大的,返回表中数据数 IF(id1>id2)THEN SET temp = id1; ELSE SET temp = id2; end if; INSERT into student VALUES(temp,password,name); select COUNT(*) INTO temp from student; ENDpublic static CallableStatement getCallableStatement(Connection conn,String sql){ try { pcstmt = conn.prepareCall(sql); } catch (SQLException e) { System.err.println("*Faild In CreateStatement By Connection"); e.printStackTrace(); } return pcstmt; }
package com.qsuron.test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Types; import com.qsuron.util.DB; public class Test3 { public static void main(String[] args) throws SQLException { Connection conn = DB.getConnection(); CallableStatement pcstmt = DB.getCallableStatement(conn,"{call p( , , , , )}"); pcstmt.setInt(1,1213400103); pcstmt.setInt(2,1213400104); pcstmt.setString(3,"123456"); pcstmt.setString(4,"qsuron"); pcstmt.registerOutParameter(5,Types.INTEGER); pcstmt.execute(); System.out.println("Return : " + pcstmt.getInt(5)); DB.close(); } }
1.XX 未完待续
二丨Batch 批处理
package com.qsuron.test;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.qsuron.util.DB;
public class Test4 {
public static void main(String[] args) throws SQLException {
Connection conn = DB.getConnection();
Statement stmt = DB.getStatement(conn);
stmt.addBatch("insert into student values ('1213400131','1','Q');");
stmt.addBatch("insert into student values ('1213400132','1','Q');");
stmt.addBatch("insert into student values ('1213400133','1','Q');");
stmt.executeBatch();
DB.close();
}
}
同理,PreparedStatement 也可使用Batch
package com.qsuron.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.qsuron.util.DB;
public class Test5 {
public static void main(String[] args) throws SQLException {
Connection conn = DB.getConnection();
PreparedStatement pstmt = DB.getPreparedState