📄 j_denglu.java
字号:
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class J_Denglu extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
public static Choice choice = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JLabel jLabel1 = null;
public static String m_strbanji=null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private Dialog dialog = null; // @jve:decl-index=0:visual-constraint="89,338"
private JButton jButton4 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JTextField jtfbanji = null;
private JTextField jtfnumber = null;
private JButton jButton5 = null;
/**
* This is the default constructor
* @throws SQLException
*/
public J_Denglu()
{
super();
initialize();
}
public static void m_banjixinxi(ResultSet r)
{
try
{
r.beforeFirst();
while(r.next())
{
System.out.println("第"+r.getRow()+"行:");
System.out.println("名称为:"+r.getString("班号"));
choice.add(r.getString("班号").toString());
}
choice.setVisible(true);
}
catch (Exception e)
{
System.err.println("异常"+e.getMessage());
}
}
private void initialize()
{
this.setSize(571, 326);
this.setLocation(new Point(200, 200));
this.setContentPane(getJContentPane());
this.setTitle("学生信息管理系统");
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(2, -1, 559, 163));
jLabel1.setIcon(new ImageIcon("老松2.jpg"));
jLabel1.setText("JLabel");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(19, 170, 115, 38));
jLabel.setBackground(new Color(142, 238, 238));
jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel.setText("请选择班级:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getChoice(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
}
return jContentPane;
}
private Choice getChoice() {
if (choice == null) {
choice = new Choice();
choice.setBounds(new Rectangle(134, 176, 217, 21));
choice.setFont(new Font("Dialog", Font.PLAIN, 18));
}
return choice;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(351, 246, 97, 36));
jButton.setText("确定");
jButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
J_StudentMessage jstdmsg=new J_StudentMessage();
jstdmsg.setVisible(true);
m_strbanji=choice.getSelectedItem();
}
});
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(448, 246, 97, 36));
jButton1.setText("取消");
try{
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
System.gc();
System.exit(0);
}
});
}
catch (Exception e) {
}
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(15, 250, 112, 33));
jButton2.setText("新增");
jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
try { getDialog().setVisible(true);}
catch (Exception e1) {}
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(125, 249, 118, 34));
jButton3.setText("删除");
jButton3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
int n=JOptionPane.showConfirmDialog(null,"确定要删除吗?","提示", JOptionPane.OK_CANCEL_OPTION);
if(n==0)
{
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:studentSqlDatabase","sa","");
Statement s=c.createStatement();
s.executeUpdate("delete from 班级信息 where 班号='"+choice.getSelectedItem()+"'");
s.executeUpdate("drop table "+choice.getSelectedItem());
s.close();
c.close();
choice.remove(choice.getSelectedItem());
JOptionPane.showMessageDialog(null,"删除成功!");
} catch(Exception e1)
{
JOptionPane.showMessageDialog(null,"删除失败!");
}
}
}
});
}
return jButton3;
}
/**
* This method initializes dialog
*
* @return java.awt.Dialog
*/
private Dialog getDialog() {
if (dialog == null) {
jLabel4 = new JLabel();
jLabel4.setBounds(new Rectangle(20, 71, 166, 37));
jLabel4.setText("请输入要添加的班级信息:");
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(20, 182, 80, 34));
jLabel3.setText("班级人数:");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(21, 131, 80, 30));
jLabel2.setText("班级名称:");
dialog = new Dialog(this);
dialog.setLayout(null);
dialog.setSize(new Dimension(305, 289));
dialog.setTitle("新增班级");
dialog.setLocation(new Point(200, 200));
dialog.add(getJButton4(), null);
dialog.add(jLabel2, null);
dialog.add(jLabel3, null);
dialog.add(jLabel4, null);
dialog.add(getJtfbanji(), null);
dialog.add(getJtfnumber(), null);
dialog.add(getJButton5(), null);
ImageIcon ic=new ImageIcon("lan.gif");
JLabel jbl=new JLabel();
jbl.setIcon(ic);
jbl.setBounds(new Rectangle(3, 24, 296, 267));
dialog.add(jbl, null);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
dialog.dispose();
}
});
}
return dialog;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setBounds(new Rectangle(45, 244, 90, 33));
jButton4.setText("确定添加");
jButton4.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
try
{
if(!jtfbanji.getText().toString().isEmpty())
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:studentSqlDatabase","sa","");
Statement s=c.createStatement();
s.executeUpdate("insert into 班级信息(班号,人数) values('"+jtfbanji.getText().toString()+"','" +
jtfnumber.getText()+"')");
s.executeUpdate("create table " +jtfbanji.getText().toString()+
"(姓名 char(20)," +
"学号 char(20) CONSTRAINT authIndex primary key," +
"年龄 integer ,"+"性别 char(20),"+"家庭住址 char(20),"
+"电话 char(20),"+"照片 char(20))");
s.close();
c.close();
JOptionPane.showMessageDialog(null,"添加成功!");
choice.add(jtfbanji.getText());
getDialog().dispose();
}
else JOptionPane.showMessageDialog(null,"请输入班级名称!");
}
catch(Exception e1)
{
System.err.println("异常"+e1.getMessage());
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
}
return jButton4;
}
/**
* This method initializes jtfbanji
*
* @return javax.swing.JTextField
*/
private JTextField getJtfbanji() {
if (jtfbanji == null) {
jtfbanji = new JTextField();
jtfbanji.setBounds(new Rectangle(104, 131, 187, 29));
}
return jtfbanji;
}
/**
* This method initializes jtfnumber
*
* @return javax.swing.JTextField
*/
private JTextField getJtfnumber() {
if (jtfnumber == null) {
jtfnumber = new JTextField();
jtfnumber.setBounds(new Rectangle(103, 186, 192, 30));
}
return jtfnumber;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setBounds(new Rectangle(155, 244, 92, 34));
jButton5.setText("取消");
jButton5.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
getDialog().dispose();
}
});
}
return jButton5;
}
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
try {
J_Denglu jdenglu=new J_Denglu();
J_linkSQL();
jdenglu.setVisible(true);
} catch (Exception e) {
}
}
private static void J_linkSQL() throws ClassNotFoundException, SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:studentSqlDatabase","sa","");
Statement s=c.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet r;
r = s.executeQuery("select * from 班级信息");
m_banjixinxi(r);
s.close();
c.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -