jdbc的一些使用

package jdbc;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;


import org.junit.Test;


import Tools.DbUtils;


public class Note {
/**
* PreparedStatement 的用法
* @throws Exception
*/
@Test
public void demo() throws Exception{
Connection conn=DbUtils.getConnection();

String sql="select * from emp where deptno=?";

PreparedStatement ps=conn.prepareStatement(sql);

//?的下標從1開始,set?的對應數據類型
ps.setInt(1, 6666);

ps.executeQuery();
}
/**
* 事務是一個運行的單元 該單元運行的程序,必須滿足acid特性
* jdbc中事務的自動提交
* jdbc事務設置爲手動提交,所有方法中的代碼在同一個事務中
* @throws SQLException 
*/
public void demo2() throws SQLException{
Connection conn = null;
PreparedStatement ps=null;

try {
conn=DbUtils.getConnection();
conn.setAutoCommit(false);  //設置手動提交事務
String sql="select * from emp where empno=33";
PreparedStatement p=conn.prepareStatement(sql);
p.executeQuery();
p.close();

String sql2="select * from emp where empno=63";
ps=conn.prepareStatement(sql2);
ps.executeQuery();

conn.commit();  //提交事務
} catch (Exception e) {
e.printStackTrace();
conn.rollback(); //回滾事務
}
conn.close();
ps.close();
}

/**
* 存儲過程的使用
* {call 存儲過程名(?,?,?)}  ?代表參數
* @throws Exception 
*/
@Test
public void demo4() throws Exception{
String sql="{call pro_add(?,?,?)}";
Connection conn=DbUtils.getConnection();

CallableStatement cs=conn.prepareCall(sql);
cs.setInt(1,33);
cs.setInt(2, 55);
cs.registerOutParameter(3, Types.INTEGER);//接收out返回值
cs.execute();

int count=cs.getInt(3);
System.out.println(count);
}
/**
* 函數的調用{ ?= call 函數名(?,?)}
* @throws Exception
*/
@Test
public void demo5() throws Exception{
String sql="{ ?=call fun_add(?,?)}";
Connection conn=DbUtils.getConnection();

CallableStatement cs=conn.prepareCall(sql);
cs.setInt(2,33);
cs.setInt(3, 55);
cs.registerOutParameter(1, Types.INTEGER);//接收out返回值
cs.execute();

int count=cs.getInt(1);
System.out.println(count);
}


static class User{
String name;
int age;
}
static List<User> list=new ArrayList<>();
static{
User user=null;
for(int i=1;i<=100;i++){
user=new User();
user.name="ww"+i;
user.age=i;
list.add(user);
}
}
/**
*批量處理
* @throws Exception 
*/
public void demo6() throws Exception{
String sql="insert into aa(name,age)  values(?,?)";
        Connection conn=DbUtils.getConnection();

PreparedStatement ps=conn.prepareStatement(sql);
for(int i=0;i<list.size();i++){
User user=list.get(i);
ps.setString(1,user.name);
ps.setInt(2, user.age);
ps.addBatch();  //打包
}
ps.executeBatch();  //執行
ps.close();
conn.close();
}

/**
* ResultSetMetaData
* 獲取列名和打印數據行
* @throws Exception
*/
@Test
public void test1() throws Exception {
Connection conn = DbUtils.getConnection();
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from dept ");


ResultSetMetaData metaData = resultSet.getMetaData();

for (int i = 0; i < metaData.getColumnCount(); i++) {
// resultSet數據下標從1開始
String columnName = metaData.getColumnName(i + 1);
int type = metaData.getColumnType(i + 1);
System.out.print(columnName + "\t");
}
System.out.println();
// 獲取數據
while (resultSet.next()) {
for (int i = 0; i < metaData.getColumnCount(); i++) {
// resultSet數據下標從1開始
System.out.print(resultSet.getString(i + 1) + "\t");
}
System.out.println();
}
statement.close();
conn.close();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章