package com.zhao.practice726_2;java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* PreparedStatement工具類
* @author Administrator
*
*/
public class DBPreparedStatement {
/**
* 驅動字符串
*/
private static final String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加載JDBC驅動
/**
* 鏈接數據庫的URL
*/
private static final String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Zhao722"; // 鏈接服務器和數據庫Zhao722
/**
* 用戶名字符串
*/
private static final String USER= "sa";
/**
* 密碼字符串
*/
private static final String PASSWORD = "3221682";
/**
* 數據庫鏈接
*/
Connection conn = null;
/**
* PreparedStatement
*/
PreparedStatement stat = null;
/**
* 結果集ResultSet
*/
ResultSet rs = null;
static {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取數據庫鏈接
* @return
*/
private Connection getConnection(){
try {
conn = DriverManager.getConnection(dbURL, USER, PASSWORD);
// System.out.println(conn);
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 得到PreparedStatement對象
* @param sql sql語句
* @return PreparedStatement
*/
public PreparedStatement getPreparedStatement(String sql){
try {
stat = getConnection().prepareStatement(sql);
System.out.println(sql);
return stat;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 關閉全部打開的數據庫鏈接
*/
public void close(){
try{
if(rs != null){
rs.close();
}
if(stat != null){
stat.close();
}
if(conn != null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
sql
package com.zhao.practice726_2;數據庫
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;服務器
public class TestPreparedStatement {
/**
* sql字符串
*/
private String sql = "select EmployeeID ,OrderDate from Orders where OrderID < ?";ide
public static void main(String[] args) {
TestPreparedStatement test = new TestPreparedStatement();
List<Order> list = test.getResult();
//for循環
for(Order o:list){
System.out.println(o);
}
}
/**
* 獲得Order集合
* @return List<Order>
*/
private List<Order> getResult(){
//獲取控制檯的輸入流
Scanner scan = new Scanner(System.in);
System.out.println("請輸入最大的訂單號碼:");
//取得輸入數字
int OrderID = scan.nextInt();
//建立List集合
List<Order> list = new ArrayList<Order>();
//建立DBPreparedStatement對象
DBPreparedStatement db = new DBPreparedStatement();
//得到PreparedStatement對象
PreparedStatement stat = db.getPreparedStatement(sql);
//取得結果集
ResultSet rs=null;
//order的引用
Order order = null;
try{
//傳入參數
stat.setInt(1, OrderID);
rs = stat.executeQuery();
//遍歷
while(rs.next()){
//建立order對象
order = new Order();
order.setCustomerID(rs.getInt(1));
order.setOrderDate(rs.getDate(2));
//加入集合中
list.add(order);
}
return list;
}catch(SQLException e){
e.printStackTrace();
}finally{
db.close();
}
return null;
}
}
工具
package com.zhao.practice726_2;sqlserver
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;測試
/**
* 測試批處理類
* @author Administrator
*
*/
public class TestBatch {
/**
* sql字符串
*/
private String sql = "insert into test1(id) values(?)";
public static void main(String[] args) {
TestBatch test = new TestBatch();
if(test.getResult()){
System.out.println("你的批處理驗證成功!!!");
}
}
/**
* 獲得是否處理成功
* @return boolean
*/
private boolean getResult(){
//獲取控制檯的輸入流
Scanner scan = new Scanner(System.in);
System.out.println("請輸入你要插入的行數:");
//取得輸入數字
int line = scan.nextInt();
//建立DBPreparedStatement對象
DBPreparedStatement db = new DBPreparedStatement();
//得到PreparedStatement對象
PreparedStatement stat = db.getPreparedStatement(sql);
try{
for(int i = 1;i<line;i++){
stat.setInt(1, i);
stat.addBatch();
}
stat.executeBatch();
return true;
}catch(SQLException e){
e.printStackTrace();
}finally{
db.close();
}
return false;
}
}
.net
package com.zhao.practice726_2;server
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 測試批處理類
* @author Administrator
*
*/
public class TestBatch {
/**
* sql字符串
*/
private String sql = "insert into test1(id) values(?)";
public static void main(String[] args) {
TestBatch test = new TestBatch();
if(test.getResult()){
System.out.println("你的批處理驗證成功!!!");
}
}
/**
* 獲得是否處理成功
* @return boolean
*/
private boolean getResult(){
//獲取控制檯的輸入流
Scanner scan = new Scanner(System.in);
System.out.println("請輸入你要插入的行數:");
//取得輸入數字
int line = scan.nextInt();
//建立DBPreparedStatement對象
DBPreparedStatement db = new DBPreparedStatement();
//得到PreparedStatement對象
PreparedStatement stat = db.getPreparedStatement(sql);
try{
for(int i = 1;i<line;i++){
stat.setInt(1, i);
stat.addBatch();
}
stat.executeBatch();
return true;
}catch(SQLException e){
e.printStackTrace();
}finally{
db.close();
}
return false;
}
}
package com.zhao.practice726_2;
import java.util.Date;
/**
* Order類的JavaBean
* @author Administrator * */ public class Order { private int CustomerID ; private Date OrderDate; public int getCustomerID() { return CustomerID; } public void setCustomerID(int customerID) { CustomerID = customerID; } public Date getOrderDate() { return OrderDate; } public void setOrderDate(Date orderDate) { OrderDate = orderDate; } @Override public String toString() { return CustomerID+"'\t'"+OrderDate; } }