JAVA小程序简单学籍系统参考代码,登陆小程序,Jtree //Jtree,JDBC,Jframe

                                            JAVA小程序简单学籍系统//Jtree,JDBC,Jframe

我们可以先在JAVA写一个类打开数据库获取连接

package sql;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.SQLException;  
  public class DBHelper {  
  public static final String url = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=UTC";  
   public static final String name = "com.mysql.cj.jdbc.Driver";  
    public static final String user = "root";  
    public static final String password = "151879rh";  
    public Connection conn = null;  
    public PreparedStatement pst = null;  
  
    public DBHelper(String sql) {  
        try {  
            Class.forName(name); 
            conn = DriverManager.getConnection(url, user, password);  
            pst = conn.prepareStatement(sql);        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
   
    public void close() {  
        try {  
            this.conn.close();  
            this.pst.close();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}

然后开始写Jframe登陆窗口

package sql;

import java.awt.Component;
import java.awt.Event;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.security.auth.kerberos.KeyTab;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class jFrame extends JDialog implements ActionListener {
	private JButton loginButton, cancelButton;
	private JLabel uidLabel, pwdLabel;
	private JTextField uidField;
	private JPasswordField pwdField;
 
	public jFrame() {
		this.setSize(320, 240);
		this.setTitle("系统登陆");
		this.setResizable(false);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setLayout(null);
 
	
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(null, "是否要退出") == 0) {
					System.exit(0);
				}
			}
		});
		initComponents();
	}
 
	
	private void initComponents() {
		loginButton = new JButton("登陆");
		loginButton.setBounds(70, 160, 80, 30);
		this.add(loginButton);
 
		cancelButton = new JButton("取消");
		cancelButton.setBounds(170, 160, 80, 30);
		this.add(cancelButton);
 
		uidLabel = new JLabel("用户名:");
		uidLabel.setBounds(50, 50, 60, 30);
		this.add(uidLabel);
 
		pwdLabel = new JLabel("密  码:");
		pwdLabel.setBounds(50, 100, 60, 30);
		this.add(pwdLabel);
 
		uidField = new JTextField();
		uidField.setBounds(120, 50, 120, 30);
		this.add(uidField);
 
		pwdField = new JPasswordField();
		pwdField.setBounds(120, 100, 120, 30);
		this.add(pwdField);
 
		Font font = new Font("微软雅黑", Font.PLAIN, 14);
		for (Component c : this.getContentPane().getComponents()) {
			c.setFont(font);
			if (c instanceof JButton) {
				((JButton) c).addActionListener(this);
			}
		}
		
		
		uidField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == 10){
					pwdField.requestFocus();
				}
			}
		});
		pwdField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				System.out.println(e.getKeyCode());
				if (e.getKeyCode() == 10) {
					loginButton.doClick();
				}
			}
		});
		
	}
	
	public static void main(String[] args) {
		new jFrame().setVisible(true);
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == loginButton) {
			String username = uidField.getText();
			String password = new String(pwdField.getPassword());
			if (username.equals("admin") && password.equals("123456")) {
				this.setVisible(false);
				new newtree(this).setVisible(true);
			} else {
				JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误",
						JOptionPane.ERROR_MESSAGE);
			}
		} else if (e.getSource() == cancelButton) {
			System.exit(0);
		}		
	}
}

最后写一个Jtree将学生信息装进去,通过mysql来获取

package sql;
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.SQLException;
import javax.swing.JTextField;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class newtree extends JFrame implements TreeSelectionListener
{   JTextArea text=new JTextArea(100,100);
    JTree tree=null;
      private jFrame login;
        public newtree(jFrame login) 
        {    this.login = login;
        	Container  contentpane=getContentPane();
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("信息与计算机学院");
        DefaultMutableTreeNode node1=new DefaultMutableTreeNode("电子信息");
        DefaultMutableTreeNode node2=new DefaultMutableTreeNode("物流工程");
        DefaultMutableTreeNode node3=new DefaultMutableTreeNode("网络工程");
        DefaultMutableTreeNode node4=new DefaultMutableTreeNode("通信工程");
        root.add(node1); root.add(node2);  root.add(node3);  root.add(node4);  
         DefaultMutableTreeNode leafnode=new DefaultMutableTreeNode("16电子一班");
         node1.add(leafnode);
         leafnode=new DefaultMutableTreeNode("16电子二班");
          node1.add(leafnode);
          leafnode=new DefaultMutableTreeNode("16物流一班");
           node2.add(leafnode);
           leafnode=new DefaultMutableTreeNode("16网络一班");
          node3.add(leafnode);
            leafnode=new DefaultMutableTreeNode("16通信一班");
            node4.add(leafnode);
           leafnode=new DefaultMutableTreeNode("16通信二班");
           node4.add(leafnode);
          
           tree =new JTree(root);
          JScrollPane scrollpane=new  JScrollPane(text);
          scrollpane.setHorizontalScrollBarPolicy(
        		JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        		  scrollpane.setVerticalScrollBarPolicy(
        		  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          
               JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,tree,scrollpane); 
          
           contentpane.add(splitpane);
           tree.addTreeSelectionListener(this);
           pack();
           setVisible(true);setBounds(30,30,500,500);
           
           
        }

        static String sql = null;  
        static DBHelper db1 = null;  
        static ResultSet ret = null;  
       
   public void valueChanged(TreeSelectionEvent eo) 
   { 
   if(eo.getSource()==tree)
   {
    DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
    if(node.isLeaf())
    {String str=node.toString();
   

    if(str.equals("16电子一班"))
   
    	 {  text.setText("");
    	    sql = "select *from tb_dianzi1";//SQL语句  
    	    db1 = new DBHelper(sql);//创建DBHelper对象  
    	
    	  try {  
    		
    	        ret = db1.pst.executeQuery();//执行语句,得到结果集  
    	        while (ret.next()) {  
    	            String uid = ret.getString(1);  
    	            String ufname = ret.getString(2);  
    	            String uxuehao = ret.getString(3);
    	            String usex = ret.getString(4);
    	            text.append(uid + "\t" + ufname + "\t"  + uxuehao  + "\t"+ usex+"\n");
    	           
    	         
    	        }  
    	        ret.close();  
    	        db1.close();//关闭连接  
    	    } catch (SQLException e) {  
    	        e.printStackTrace();  
    	    }  
    	 
    	 
    	 }
        
    	 
     
     if(str.equals("16电子二班"))
    	   
	 {   
    	 text.setText("");

    	 sql = "select *from tb_dianzi2";  
	    db1 = new DBHelper(sql); 
	   
	  try {  
		
	        ret = db1.pst.executeQuery(); 
	        while (ret.next()) {  
	        	
	            String uid = ret.getString(1);  
	            String ufname = ret.getString(2);  
	            String uxuehao = ret.getString(3);
	            String usex = ret.getString(4);
	            
	       	   

	            text.append(uid + "\t" + ufname + "\t"  + uxuehao  + "\t"+ usex+"\n");
	           
	          
	        } 
	        ret.close();  
	        db1.close();  
	    } catch (SQLException e) {  
	        e.printStackTrace();  
	    }  
	 }
	 
	 
	  if(str.equals("16通信一班"))
		   
 	 {  text.setText("");
 	    sql = "select *from tb_tongxin1";  
 	    db1 = new DBHelper(sql); 
 	
 	  try {  
 		
 	        ret = db1.pst.executeQuery(); 
 	        while (ret.next()) {  
 	            String uid = ret.getString(1);  
 	            String ufname = ret.getString(2);  
 	            String uxuehao = ret.getString(3);
 	            String usex = ret.getString(4);
 	            text.append(uid + "\t" + ufname + "\t"  + uxuehao  + "\t"+ usex+"\n");
 	           
 	         
 	        } 
 	        ret.close();  
 	        db1.close(); 
 	    } catch (SQLException e) {  
 	        e.printStackTrace();  
 	    }  
 	 }
 	 
 	  
 	  if(str.equals("16通信二班"))
		   
  	 {  text.setText("");
  	    sql = "select *from tb_tongxin2"; 
  	    db1 = new DBHelper(sql);  
  	
  	  try {  
  		
  	        ret = db1.pst.executeQuery();
  	        while (ret.next()) {  
  	            String uid = ret.getString(1);  
  	            String ufname = ret.getString(2);  
  	            String uxuehao = ret.getString(3);
  	            String usex = ret.getString(4);
  	            text.append(uid + "\t" + ufname + "\t"  + uxuehao  + "\t"+ usex+"\n");
  	           
  	         
  	        }
  	        ret.close();  
  	        db1.close();  
  	    } catch (SQLException e) {  
  	        e.printStackTrace();  
  	    }  
  	 }
 	  
 	  
  	 if(str.equals("16物流一班"))
		   
  	 {  text.setText("");
  	    sql = "select *from tb_wuliu1"; 
  	    db1 = new DBHelper(sql); 
  	
  	  try {  
  		
  	        ret = db1.pst.executeQuery();  
  	        while (ret.next()) {  
  	            String uid = ret.getString(1);  
  	            String ufname = ret.getString(2);  
  	            String uxuehao = ret.getString(3);
  	            String usex = ret.getString(4);
  	            text.append(uid + "\t" + ufname + "\t"  + uxuehao  + "\t"+ usex+"\n");
  	           
  	         
  	        } 
  	        ret.close();  
  	        db1.close();//关闭连接  
  	    } catch (SQLException e) {  
  	        e.printStackTrace();  
  	    }  
  	 }
  	  
  	  
  	if(str.equals("16网络一班"))
		   
 	 {  text.setText("");
 	    sql = "select *from tb_wangluo";  
 	    db1 = new DBHelper(sql); 	
 	  try {  
 		
 	        ret = db1.pst.executeQuery(); 
 	        while (ret.next()) {  
 	            String uid = ret.getString(1);  
 	            String ufname = ret.getString(2);  
 	            String uxuehao = ret.getString(3);
 	            String usex = ret.getString(4);
 	            text.append(uid + "\t" + ufname + "\t"  + uxuehao  + "\t"+ usex+"\n");
 	           
 	         
 	        }
 	        ret.close();  
 	        db1.close(); 
 	    } catch (SQLException e) {  
 	        e.printStackTrace();  
 	    }  
 	 }
 		 }
   
}
   }
 
    public static void main(String[] args) { 
    	new tree();
    	
}  
}

将三个类放进一个包里运行,默认使用的是package sql,是学校当时学java时候的一个小作业,都是基本的东西,供初学者参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值