java解决软件工程复试题目

DataBaseConnection.java

 


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DataBaseConnection
{


static //导入驱动
{
try
{
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
}
catch(IllegalAccessException e1)
{
System.out.println("访问异常");
}
catch(ClassNotFoundException e2)
{
System.out.println("驱动类找不到");
}
catch(InstantiationException e3)
{
System.out.println("实例异常");
}
}

public static Connection getConn() //连接对象
{
try
{
//String url="jdbc:mysql://localhost/scut?user=root&password=510";
String url="jdbc:sqlserver://localhost:1433;DatabaseName=SCNT";
//jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=SCNT
return DriverManager.getConnection(url,"lm","123456");
}
catch(SQLException e)
{
System.out.println("连接URL异常");
return null;
}
}

public static Statement getStat() //执行语句对象
{
try
{
return getConn().createStatement();
}
catch(SQLException e)
{
System.out.println("SQL语句异常");
return null;
}
}

public static int Insert(String sql) //自定义的插入语句
{
try
{
return getStat().executeUpdate(sql); //如果SQL语句成功返回int型的值
}
catch(Exception e)
{
System.out.println("SQL语句异常");
return -1;
}
}
public static int Update(String sql) //自定义的修改语句
{
try
{
return getStat().executeUpdate(sql); //如果SQL语句成功返回int型的值
}
catch(Exception e)
{
System.out.println("SQL语句异常");
return -1;
}
}

public static int Delete(String sql) //自定义的删除语句
{
try
{
return getStat().executeUpdate(sql); //如果SQL语句成功返回int型的值
}
catch(Exception e)
{
System.out.println("SQL语句异常");
return -1;
}
}

public static ResultSet Select(String sql) //自定义的查询语句
{
try
{
return getStat().executeQuery(sql); //如果SQL语句成功返回ResultSet型的值
}
catch(Exception e)
{
System.out.println("SQL语句异常");
return null;
}
}

}



Main.java

import java.awt.BorderLayout;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.sql.ResultSet;  
import java.sql.ResultSetMetaData;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.util.Vector;  
  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JMenuItem;  
import javax.swing.JPanel;  
import javax.swing.JScrollPane;  
import javax.swing.JTable;  
import javax.swing.JTextField;  
import javax.swing.table.DefaultTableModel;  
  
public class Main {  
    public static void main(String[] args) {  
        Window win = new Window();  
        win.setTitle("华南理工大学机试"); 
        
    }  
  
}  
  
class Window extends JFrame implements ActionListener {   
    JTextField text1,text2;  
    JButton button1,button2;  
    JLabel label1,label2;  
    JPanel panel;  
    JScrollPane jscrollpane;  
    JTable table;  
    DefaultTableModel dtm;  
    Statement stmt;  
    ResultSet rs;  
    ResultSetMetaData rsmd;  
    DataBaseConnection dbc = new DataBaseConnection();  
 
    Window() {  
        init();  
        setBounds(300, 100, 850, 550);  
        setVisible(true);  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  
  
    public void init() {  
        setLayout(new BorderLayout());  
        // 插入数据  
        text1 = new JTextField(6);  
        button1 = new JButton("查询一");  
        label1 = new JLabel("按学号查询dfdasfadfadfasd:");
        text2 = new JTextField(6);  
        button2 = new JButton("查询二");  
        label2 = new JLabel("按姓名查询afdasdfasdfasdfasd:");
        panel = new JPanel();  
        panel.add(label1);  
        panel.add(text1);  
        panel.add(button1); 
        panel.add(label2);  
        panel.add(text2);  
        panel.add(button2);
        button1.addActionListener(this); 
        button2.addActionListener(this);
        add(panel, BorderLayout.SOUTH);  
        showSth();
    }  
  
    public void actionPerformed(ActionEvent e) {  
  
     
        // 查询  
        if (e.getSource() == button1) {
        Vector colum = new Vector();  
            Vector rows = new Vector();
        System.out.println(text1.getText());
        String StuNo=text1.getText();
        String sql="select * from Student where StuNo="+StuNo;
    ResultSet rs=DataBaseConnection.Select(sql);
    System.out.println(rs);
            try {  
                rsmd = rs.getMetaData();  
                for (int i = 1; i <= rsmd.getColumnCount(); ++i)  
                    colum.addElement(rsmd.getColumnName(i));  
                while (rs.next()) {  
                    Vector currow = new Vector();  
                    for (int i = 1; i <= rsmd.getColumnCount(); ++i) {  
                        currow.addElement(rs.getString(i));  
                    }  
                    rows.addElement(currow);  
                }  
                table = new JTable(rows, colum);  
                add(table, BorderLayout.CENTER);  
                table.setVisible(true);  
                table.setRowHeight(50);  
                add(new JScrollPane(table), BorderLayout.CENTER);  
                // table.setFillsViewportHeight(true);  
  
            } catch (SQLException e1) {  
                // TODO Auto-generated catch block  
                e1.printStackTrace();  
            }  
            show();
  
        } 
        else if(e.getSource() == button2)
        {
        System.out.println(text2.getText());
        Vector colum = new Vector();  
            Vector rows = new Vector();
        System.out.println(text1.getText());
        String StuNo=text1.getText();
        String sql="select avg(StuAge) as '平均年龄' from Student";
    ResultSet rs=DataBaseConnection.Select(sql);
    System.out.println(rs);
            try {  
                rsmd = rs.getMetaData();  
                for (int i = 1; i <= rsmd.getColumnCount(); ++i)  
                    colum.addElement(rsmd.getColumnName(i));  
                while (rs.next()) {  
                    Vector currow = new Vector();  
                    for (int i = 1; i <= rsmd.getColumnCount(); ++i) {  
                        currow.addElement(rs.getString(i));  
                    }  
                    rows.addElement(currow);  
                }  
                table = new JTable(rows, colum);  
                add(table, BorderLayout.CENTER);  
                table.setVisible(true);  
                table.setRowHeight(50);  
                add(new JScrollPane(table), BorderLayout.CENTER);  
                // table.setFillsViewportHeight(true);  
  
            } catch (SQLException e1) {  
                // TODO Auto-generated catch block  
                e1.printStackTrace();  
            }  
            show();
  
        }
    }
    public void showSth()
    {
    Vector colum = new Vector();  
        Vector rows = new Vector();  
    String sql="select * from Student";
ResultSet rs=DataBaseConnection.Select(sql);
System.out.println(rs);
        try {  
            rsmd = rs.getMetaData();  
            for (int i = 1; i <= rsmd.getColumnCount(); ++i)  
                colum.addElement(rsmd.getColumnName(i));  
            while (rs.next()) {  
                Vector currow = new Vector();  
                for (int i = 1; i <= rsmd.getColumnCount(); ++i) {  
                    currow.addElement(rs.getString(i));  
                }  
                rows.addElement(currow);  
            }  
            table = new JTable(rows, colum);  
            add(table, BorderLayout.CENTER);  
            table.setVisible(true);  
            table.setRowHeight(50);  
            add(new JScrollPane(table), BorderLayout.CENTER);  
            // table.setFillsViewportHeight(true);  


        } catch (SQLException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
        }  
        show();  
    }
  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值