import javax.swing.*;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class guii {
static String sno , jno , pno , one;
static int quantity;
static String str1;
JFrame frame ;
JLabel label;
JTextArea text;
JTextArea text1 ,text2 ,text3 ;
JTextField t1;
JTextField t2 ,t3,t4,t5;
JTextField jtf1, jtf2, jtf3 ,jtf4 ,jtf5 ;//定义三个输入框
JLabel jlinfo;
static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
// 连接服务器和数据库ServletUser
static String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=STU";
static String userName = "11"; // 默认用户名
static String userPwd = "17"; // 密码
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
guii gui = new guii();
gui.go();
}
public void go()throws IOException {
JLabel jl1 = new JLabel("one:");
t1 = new JTextField(8);
JLabel jl2 = new JLabel("sno:");
t2 = new JTextField(8);
JLabel jl3 = new JLabel("pno:");
t3 = new JTextField(8);
JLabel jl4 = new JLabel("jno:");
t4 = new JTextField(8);
JLabel jl5 = new JLabel("qut:");
t5 = new JTextField(8);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JTextArea(30 , 40);//
text.setLineWrap(true);
JPanel panel1 = new JPanel();
JScrollPane scroller1 = new JScrollPane(text);
scroller1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel1.add(scroller1);
text1 = new JTextArea(30 ,40);//
text1.setLineWrap(true);
JPanel panel2 = new JPanel();
JScrollPane scroller2 = new JScrollPane(text1);
scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel2.add(scroller2);
text3 = new JTextArea(30 , 50);//
text3.setLineWrap(true);
JPanel panel3 = new JPanel();
JScrollPane scroller3 = new JScrollPane(text3);
scroller3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel3.add(scroller3);
JLabel jpn = new JLabel("PN表 ");
JLabel jspj = new JLabel("SPJ表 ");
JLabel jor = new JLabel(" Orders表");
Box box1 = new Box(BoxLayout.X_AXIS);
Box box2 = new Box(BoxLayout.X_AXIS);
Box box3 = new Box(BoxLayout.X_AXIS);
box1.add(jpn);
box1.add(panel1);
box1.add(jspj);
box1.add(panel2);
box1.add(jor);
box1.add(panel3);
JButton sure = new JButton("确定");
sure.addActionListener(new sureListener());
Box boxpan = new Box(BoxLayout.Y_AXIS);
boxpan.add(box1);
boxpan.add(box3);
boxpan.add(box2);
Box boxt = new Box(BoxLayout.X_AXIS) ;
boxt.add(jl1);
boxt.add(t1);
boxt.add(jl2);
boxt.add(t2);
boxt.add(jl3);
boxt.add(t3);
boxt.add(jl4);
boxt.add(t4);
boxt.add(jl5);
boxt.add(t5);
boxt.add(sure);
// frame.getContentPane().add(BorderLayout.SOUTH,panel5);
frame.getContentPane().add(BorderLayout.CENTER,boxpan);
frame.getContentPane().add(BorderLayout.NORTH,boxt);
// frame.getContentPane().add(BorderLayout.EAST,panel3);
frame.setSize(100, 100);
frame.setVisible(true);
}
class sureListener implements ActionListener{
public void actionPerformed(ActionEvent event){
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);
text.append("SNO PNO BALANCE\n" );
while(rs.next()){
text.append(rs.getString("SNO")+" "+rs.getString("PNO")+" "+rs.getInt("BALANCE") );
text.append("\n");
}
// text.append("查询数据成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
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);
text1.append("SNO PNO JNO QTY\n" );
while(rs.next()){
text1.append(rs.getString("SNO")+" "+ rs.getString("PNO")+" "+rs.getString("JNO")+" "+rs.getInt("QTY") +"\n");
}
// System.out.println("查询数据成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
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);
text3.append("ONE SNO PNO JNO OTIME QUANTITY\n" );
while(rs.next()){
text3.append(rs.getString("ONE")+" "+rs.getString("SNO")+" "+rs.getString("PNO")+" "+rs.getString("JNO")
+" "+ rs.getDate("OTIME") +" "+ rs.getInt("QUANTITY")+"\n" );
}
// System.out.println("查询数据成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
CallableStatement callStmt = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
// 存储过程 PTransfer 其实是向数据库插入一条数据
callStmt = conn.prepareCall("{call PTransfer(?,?,?,?,?)}");
// 参数index从1开始,依次 1,2,3...
String testONE = t1.getText();
callStmt.setString(1, testONE);
String testSNO = t2.getText();
callStmt.setString(2, testSNO);
String testPNO = t3.getText();
callStmt.setString(3, testPNO);
String testJNO = t4.getText();
callStmt.setString(4, testJNO);
String testQTY = t5.getText();
callStmt.setString(5, testQTY);
callStmt.execute();
System.out.println("------- Test End.");
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (callStmt != null) {
try {
callStmt.close();//stmt一开始就是null的话,如果不判断就一直出SQL异常,所以应该先判断一下
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block如果stmt一开始就是null的话,如果不判断就一直出SQL异常,所以应该先判断一下
e.printStackTrace();
System.out.printf("error");
}
}
}
}}}
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class guii {
static String sno , jno , pno , one;
static int quantity;
static String str1;
JFrame frame ;
JLabel label;
JTextArea text;
JTextArea text1 ,text2 ,text3 ;
JTextField t1;
JTextField t2 ,t3,t4,t5;
JTextField jtf1, jtf2, jtf3 ,jtf4 ,jtf5 ;//定义三个输入框
JLabel jlinfo;
static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
// 连接服务器和数据库ServletUser
static String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=STU";
static String userName = "11"; // 默认用户名
static String userPwd = "17"; // 密码
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
guii gui = new guii();
gui.go();
}
public void go()throws IOException {
JLabel jl1 = new JLabel("one:");
t1 = new JTextField(8);
JLabel jl2 = new JLabel("sno:");
t2 = new JTextField(8);
JLabel jl3 = new JLabel("pno:");
t3 = new JTextField(8);
JLabel jl4 = new JLabel("jno:");
t4 = new JTextField(8);
JLabel jl5 = new JLabel("qut:");
t5 = new JTextField(8);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text = new JTextArea(30 , 40);//
text.setLineWrap(true);
JPanel panel1 = new JPanel();
JScrollPane scroller1 = new JScrollPane(text);
scroller1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel1.add(scroller1);
text1 = new JTextArea(30 ,40);//
text1.setLineWrap(true);
JPanel panel2 = new JPanel();
JScrollPane scroller2 = new JScrollPane(text1);
scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel2.add(scroller2);
text3 = new JTextArea(30 , 50);//
text3.setLineWrap(true);
JPanel panel3 = new JPanel();
JScrollPane scroller3 = new JScrollPane(text3);
scroller3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
panel3.add(scroller3);
JLabel jpn = new JLabel("PN表 ");
JLabel jspj = new JLabel("SPJ表 ");
JLabel jor = new JLabel(" Orders表");
Box box1 = new Box(BoxLayout.X_AXIS);
Box box2 = new Box(BoxLayout.X_AXIS);
Box box3 = new Box(BoxLayout.X_AXIS);
box1.add(jpn);
box1.add(panel1);
box1.add(jspj);
box1.add(panel2);
box1.add(jor);
box1.add(panel3);
JButton sure = new JButton("确定");
sure.addActionListener(new sureListener());
Box boxpan = new Box(BoxLayout.Y_AXIS);
boxpan.add(box1);
boxpan.add(box3);
boxpan.add(box2);
Box boxt = new Box(BoxLayout.X_AXIS) ;
boxt.add(jl1);
boxt.add(t1);
boxt.add(jl2);
boxt.add(t2);
boxt.add(jl3);
boxt.add(t3);
boxt.add(jl4);
boxt.add(t4);
boxt.add(jl5);
boxt.add(t5);
boxt.add(sure);
// frame.getContentPane().add(BorderLayout.SOUTH,panel5);
frame.getContentPane().add(BorderLayout.CENTER,boxpan);
frame.getContentPane().add(BorderLayout.NORTH,boxt);
// frame.getContentPane().add(BorderLayout.EAST,panel3);
frame.setSize(100, 100);
frame.setVisible(true);
}
class sureListener implements ActionListener{
public void actionPerformed(ActionEvent event){
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);
text.append("SNO PNO BALANCE\n" );
while(rs.next()){
text.append(rs.getString("SNO")+" "+rs.getString("PNO")+" "+rs.getInt("BALANCE") );
text.append("\n");
}
// text.append("查询数据成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
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);
text1.append("SNO PNO JNO QTY\n" );
while(rs.next()){
text1.append(rs.getString("SNO")+" "+ rs.getString("PNO")+" "+rs.getString("JNO")+" "+rs.getInt("QTY") +"\n");
}
// System.out.println("查询数据成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
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);
text3.append("ONE SNO PNO JNO OTIME QUANTITY\n" );
while(rs.next()){
text3.append(rs.getString("ONE")+" "+rs.getString("SNO")+" "+rs.getString("PNO")+" "+rs.getString("JNO")
+" "+ rs.getDate("OTIME") +" "+ rs.getInt("QUANTITY")+"\n" );
}
// System.out.println("查询数据成功");
rs.close();
stmt1.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
CallableStatement callStmt = null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
// 存储过程 PTransfer 其实是向数据库插入一条数据
callStmt = conn.prepareCall("{call PTransfer(?,?,?,?,?)}");
// 参数index从1开始,依次 1,2,3...
String testONE = t1.getText();
callStmt.setString(1, testONE);
String testSNO = t2.getText();
callStmt.setString(2, testSNO);
String testPNO = t3.getText();
callStmt.setString(3, testPNO);
String testJNO = t4.getText();
callStmt.setString(4, testJNO);
String testQTY = t5.getText();
callStmt.setString(5, testQTY);
callStmt.execute();
System.out.println("------- Test End.");
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (callStmt != null) {
try {
callStmt.close();//stmt一开始就是null的话,如果不判断就一直出SQL异常,所以应该先判断一下
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block如果stmt一开始就是null的话,如果不判断就一直出SQL异常,所以应该先判断一下
e.printStackTrace();
System.out.printf("error");
}
}
}
}}}