jdbc連線資料庫,並查詢,呼叫儲存方法
阿新 • • 發佈:2018-12-09
package SQL;
import java.awt.event.ActionEvent;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SQL_STU {
static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 載入JDBC驅動
// 連線伺服器和資料庫ServletUser
static String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=STU";
static String userName = "151"; // 預設使用者名稱
static String userPwd = "17"; // 密碼
public void connSQL(){//判斷是否與資料庫連線
Connection conn = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); // 如果連線成功
} catch (Exception e) {
e.printStackTrace();
}finally{
//進行資源的釋放
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public void showSP(){//將SP表全部記錄輸出
Connection conn = null;
try{//查詢語句
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
String query="SELECT * FROM SP";// 創造SQL語句
Statement stmt1 =conn.createStatement();// 執行SQL語句
ResultSet rs=stmt1.executeQuery(query);
System.out.println("SNO PNO BALANCE" );
while(rs.next()){
System.out.println(rs.getString("SNO")+rs.getString("PNO")+rs.getInt("BALANCE") );
}
System.out.println("查詢資料成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void showSPJ(){//將SPJ表全部記錄輸出
Connection conn = null;
try{//查詢語句
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
String query="SELECT * FROM SPJ";// 創造SQL語句
Statement stmt1 =conn.createStatement();// 執行SQL語句
ResultSet rs=stmt1.executeQuery(query);
System.out.println("SNO PNO JNO QTY" );
while(rs.next()){
System.out.println(rs.getString("SNO")+rs.getString("PNO")+""+rs.getString("JNO")+" "+rs.getInt("QTY") );
}
System.out.println("查詢資料成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void showOrders(){//將Orders表的全部記錄輸出
Connection conn = null;
try{//查詢語句
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
String query="SELECT * FROM Orders";// 創造SQL語句
Statement stmt1 =conn.createStatement();// 執行SQL語句
ResultSet rs=stmt1.executeQuery(query);
System.out.println("ONE SNO PNO JNO OTIME QUANTITY" );
while(rs.next()){
System.out.println(rs.getString("ONE")+rs.getString("SNO")+rs.getString("PNO")+""+rs.getString("JNO")
+" "+ rs.getDate("OTIME") +" "+ rs.getInt("QUANTITY") );
}
System.out.println("查詢資料成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void PTransfer() throws SQLException{//呼叫儲存過程 PTransfer,給Orders表輸入資料
Connection conn = null;
CallableStatement callStmt = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
// 儲存過程 PTransfer 其實是向資料庫插入一條資料
callStmt = conn.prepareCall("{call PTransfer(?,?,?,?,?)}");
// 引數index從1開始,依次 1,2,3...
Scanner in = new Scanner(System.in);
System.out.println("請輸入ONE");
String testONE = in.next();
callStmt.setString(1, testONE);
System.out.println("請輸入SN0");
String testSNO = in.next();
callStmt.setString(2, testSNO);
System.out.println("請輸入PN0");
String testPNO = in.next();
callStmt.setString(3, testPNO);
System.out.println("請輸入JN0");
String testJNO = in.next();
callStmt.setString(4, testJNO);
System.out.println("請輸入QUANTITY");
int testQTY = in.nextInt();
callStmt.setInt(5, testQTY);
callStmt.execute();
System.out.println("------- Test End.");
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (callStmt != null) {
callStmt.close();
}
if (conn != null) {
conn.close();
}
}
}
public static void main(String[] args) throws SQLException {
SQL_STU sql = new SQL_STU();
sql.connSQL();
// sql.showOrders();
sql.showSP();
//建立gui 有5個文字框輸入資料 三個文字框輸出sp spj orders表 並有兩個按鈕 一個確定一個退出
}
}
import java.awt.event.ActionEvent;
import java.sql.CallableStatement;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SQL_STU {
// 連線伺服器和資料庫ServletUser
static String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=STU";
static String userName = "151"; // 預設使用者名稱
static String userPwd = "17"; // 密碼
public void connSQL(){//判斷是否與資料庫連線
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); // 如果連線成功
} catch (Exception e) {
e.printStackTrace();
}finally{
//進行資源的釋放
if(conn!=null){
try {
conn.close();
e.printStackTrace();
}
}
}
}
public void showSP(){//將SP表全部記錄輸出
Connection conn = null;
try{//查詢語句
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
String query="SELECT * FROM SP";// 創造SQL語句
Statement stmt1 =conn.createStatement();// 執行SQL語句
ResultSet rs=stmt1.executeQuery(query);
System.out.println("SNO PNO BALANCE" );
while(rs.next()){
System.out.println(rs.getString("SNO")+rs.getString("PNO")+rs.getInt("BALANCE") );
}
System.out.println("查詢資料成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void showSPJ(){//將SPJ表全部記錄輸出
Connection conn = null;
try{//查詢語句
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
String query="SELECT * FROM SPJ";// 創造SQL語句
Statement stmt1 =conn.createStatement();// 執行SQL語句
ResultSet rs=stmt1.executeQuery(query);
System.out.println("SNO PNO JNO QTY" );
while(rs.next()){
System.out.println(rs.getString("SNO")+rs.getString("PNO")+""+rs.getString("JNO")+" "+rs.getInt("QTY") );
}
System.out.println("查詢資料成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void showOrders(){//將Orders表的全部記錄輸出
Connection conn = null;
try{//查詢語句
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
String query="SELECT * FROM Orders";// 創造SQL語句
Statement stmt1 =conn.createStatement();// 執行SQL語句
ResultSet rs=stmt1.executeQuery(query);
System.out.println("ONE SNO PNO JNO OTIME QUANTITY" );
while(rs.next()){
System.out.println(rs.getString("ONE")+rs.getString("SNO")+rs.getString("PNO")+""+rs.getString("JNO")
+" "+ rs.getDate("OTIME") +" "+ rs.getInt("QUANTITY") );
}
System.out.println("查詢資料成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void PTransfer() throws SQLException{//呼叫儲存過程 PTransfer,給Orders表輸入資料
Connection conn = null;
CallableStatement callStmt = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
// 儲存過程 PTransfer 其實是向資料庫插入一條資料
callStmt = conn.prepareCall("{call PTransfer(?,?,?,?,?)}");
// 引數index從1開始,依次 1,2,3...
Scanner in = new Scanner(System.in);
System.out.println("請輸入ONE");
String testONE = in.next();
callStmt.setString(1, testONE);
System.out.println("請輸入SN0");
String testSNO = in.next();
callStmt.setString(2, testSNO);
System.out.println("請輸入PN0");
String testPNO = in.next();
callStmt.setString(3, testPNO);
System.out.println("請輸入JN0");
String testJNO = in.next();
callStmt.setString(4, testJNO);
System.out.println("請輸入QUANTITY");
int testQTY = in.nextInt();
callStmt.setInt(5, testQTY);
callStmt.execute();
System.out.println("------- Test End.");
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (callStmt != null) {
callStmt.close();
}
if (conn != null) {
conn.close();
}
}
}
public static void main(String[] args) throws SQLException {
SQL_STU sql = new SQL_STU();
sql.connSQL();
// sql.showOrders();
sql.showSP();
//建立gui 有5個文字框輸入資料 三個文字框輸出sp spj orders表 並有兩個按鈕 一個確定一個退出
}
}