jdbc 數據的增刪改查的Statement Resultset PreparedStatement


完成數據庫的連接,就馬上要對數據庫進行增刪改查操作了;先來了解一下Statement

 

通過JDBC插入數據 (這里提供一個查找和插入方法)

Statement:用於執行sql語句的對象;
*1.通過Connection 的creatStatement()方法來獲取;
*2.通過executeUpdate(sql) 可以執行SQL語句
*3.傳入的SQL可以是insert update delete,但是不能是select;
* 注意:在使用后要關閉connection和statement(在finally中關閉)
* 關閉的順序:先關statement ; 后關 connection

 

@Test
public void testStatement()throws Exception{
//1.獲取數據庫連接
Connection conn = null;
Statement st = null;
try {
conn = getConnection();//參看最底下的附錄(獲取連接)
//2.准備插入的sql
String sql ="insert into contacts(name,address,phone)"+"value('xyz','abc','abc')";
//3.執行插入
//獲取sql的statement對象:Connection的createStatement()方法來獲取
st = conn.createStatement();
//調用statement對象的executeUpdate(sql)對象,插入sql語句
st.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(st !=null)
//關閉Statement 對象
st.close();
}catch (Exception e) {
e.printStackTrace();
}finally{
//關閉連接
if(conn !=null)
conn.close();
}

}


}

/**
* ResultSet:結果集,封裝了使用JDBC進行查詢的接口
* 1.調用Statement 對象的executeQuesry(sql)可以得到結果集
* 2.返回一張數據表,有指針指向數據表的第一行的前面
* 3.可以調用next方法檢測下一行是否有效,若有效返回true且指針下移
* 4.當指針對到一行時,可以通過getxxxx(index)或者getxxx(cloumName);
* 獲取每一列的值:getInt(1),getString("name")
* @throws Exception
*/
@Test
public void testResultSet() throws Exception{
//獲取Id=19的記錄值,並且每條打印
Connection con = null;
ResultSet rs = null;
Statement st = null;
try {
//1.獲取connection
con = JdbcTools.getConnection();//JdbcTools.getConnection()同附錄的方法
//2.獲取Statement
st = con.createStatement();
//3.准備sql
String sql = "select * from contacts where id=19";
//4.獲取ResultSet
rs = st.executeQuery(sql);
//5.處理ResultSet
if(rs.next()){//如果是多條記錄,把if改成while
int id = rs.getInt(1);//第一列
String name = rs.getString("name");
String address = rs.getString(3);
System.out.println(id);
System.out.println(name);
System.out.println(address);
}
//6.關閉
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcTools.release(rs, st, con);//方法見附錄
}
}

 

//以上提供了插入和查詢方法 

*********************************************************************************************************************

 這里我們在來了解一個PreparedStatement方法,可以對比Stetement

/**
* PreparedStatement是Stetement的子接口;
*preparedStatement:使用statement,sql需要拼接;
*PerparedStatement:String sql="insert into contacts(name,address,phone) value(?,?,?)"
*Statement:String sql = "insert into contacts(name,address,phone)"+"value('nn'+'121'+'1212')";
*1.創建preparedStatement:
*2.調動PreparedStatement的setxxx(int index,object value),設置占位符的值
*這里index從1開始
*3.執行sql語句:executeQuery()或者executeUpdate(),前者是查詢,后者是更新
*注意,執行時不再需要闖入sql語句
*
*好處:1防止SQl注入,即防止拼接過程中有加入其它東西,例如有OR···,這樣,sql就可以正確執行;2提高性能;3····
*
* @throws Exception
*/
@Test
public void testPreparedStatement() throws Exception {
Connection con = null;
PreparedStatement p = null;
ResultSet rs = null;
try {
con = JdbcTools.getConnection();
String sql = " insert into contacts(name,address,phone)values(?,?,?)";
p = con.prepareStatement(sql);
p.setString(1, "tom");
p.setString(2, "zhejiang");
p.setString(3, "15988324290");
p.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcTools.releaseDB(rs, p, con);
}

}

 

 

附上

public Connection getConnection() throws Exception{
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;

//讀取類路徑下的properties
InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties p = new Properties();
p.load(in);
driverClass=p.getProperty("driver");
jdbcUrl = p.getProperty("jdbcUrl");
user = p.getProperty("user");
password = p.getProperty("password");


Driver driver = (Driver) Class.forName(driverClass).newInstance();//實例化
Properties info = new Properties();
info.put("user",user);
info.put("password",password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}

 

 

 

public static void release(ResultSet rs, Statement statement ,Connection con) throws Exception{

 

if(rs != null){

 

try {

 

rs.close();

 

} catch (Exception e2) {

 

e2.printStackTrace();

 

}

 

 

 

}

 

 

 

if(statement !=null){

 

try {

 

statement.close();

 

} catch (Exception e2) {

 

e2.printStackTrace();

 

}

 

}

 

 

 

if(con!=null){

 

try {

 

con.close();

 

} catch (Exception e2) {

 

e2.printStackTrace();

 

}

 

}

 

}

 

public static void releaseDB(ResultSet rs, PreparedStatement statement ,Connection con) throws Exception{

if(rs != null){

try {

rs.close();

} catch (Exception e2) {

e2.printStackTrace();

}

 

}

 

if(statement !=null){

try {

statement.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

 

if(con!=null){

try {

con.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

 

需要導入的包

mysql-connector-java-5.1.34.jar

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM