(剛入門級別)java課程設計——學生信息管理系統

在快到期末的時候老師叫我們寫一個java的課程設計,並要求至少8個頁面。由於在學習java中並不是很系統的學習,只是在上課時簡單的聽聽,所以我對java還是很陌生。在寫這個項目的時候參考書本及一些大佬的文章,理解如何連接MySQL數據庫,終於用了一週的時間才完成這個簡單的項目,由於個人能力不足,請各位大佬理解一下。

話不多說,請看圖

登錄界面

登錄

主界面

在這裏插入圖片描述

添加功能

在這裏插入圖片描述

刪除功能

在這裏插入圖片描述

查詢功能

在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

修改功能

在這裏插入圖片描述

添加管理員、修改密碼

在這裏插入圖片描述在這裏插入圖片描述

部分源代碼

登錄界面代碼

package Student;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.MatteBorder;

public class Login extends JFrame {
	
	static  JTextField t1 ; //文本框
			JTextField t2 ;
	
	
	public Login() {
	Container cp = getContentPane();
	
	JButton b1 ,b2;    //按鈕
	Label l1 , l2;      //標籤
	t1 = new JTextField(15);
	t2 =new JPasswordField(15);
	b1 = new JButton("登陸");
    b2 = new JButton("重置");
    l1 = new Label("用戶名: ");
	l2 = new Label("密碼    : ");
	
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	JPanel p3 = new JPanel();
	JPanel p4 = new JPanel();
	JPanel p5 = new JPanel();

	p1.add(l1);
	p1.add(t1);
	p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
	p1.setBackground(Color.YELLOW); //設置此組件的背景色。
	
	p2.add(l2);
	p2.add(t2);
	p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
	p2.setBackground(Color.YELLOW);
	
	p3.add(b1);
	p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
	p3.setBackground(Color.yellow);
	
	p4.add(b2);
	p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
	p4.setBackground(Color.yellow);
	
	 p5.add(p1);
	 p5.add(p2);
	 p5.add(p3);
	 p5.add(p4);
	 p5.setLayout(new FlowLayout(FlowLayout.CENTER,60,30)); //居中,水平距60,垂直距30
	 cp.add(p5); 
	 
	 //監視器
	 b1.addActionListener(new ActionListener (){
			public void actionPerformed(ActionEvent e) {
				if( DAO.DengLu(t1.getText(), t2.getText()) ){
				//JOptionPane 有助於方便地彈出要求用戶提供值或向其發出通知的標準對話框。
					JOptionPane.showMessageDialog(t1,"登錄成功");  //調出標題爲 "Message" 的信息消息對話框
					Operate h1 = new Operate();					
					dispose();  //釋放由此 Window、其子組件及其擁有的所有子組件所使用的所有本機屏幕資源
				}
				else {
					JOptionPane.showMessageDialog(null,"用戶名或密碼錯誤","錯誤",JOptionPane.ERROR_MESSAGE);	
					t1.setText("");
					t2.setText("");
					}
				}
					
			});
	 
	 b2.addActionListener(new ActionListener (){
			@Override
			public void actionPerformed(ActionEvent e) {
				t1.setText("");
				t2.setText("");
				t1.requestFocusInWindow();	//請求此 Component 獲取輸入焦點
			}
		});
	}
}

主頁面代碼

package Student;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

public class Operate extends JFrame{
	
	
	public Operate() {
		
		setBounds(600,300,340,400);
		setVisible(true);
		setTitle("學生信息管理系統");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false); //窗口不可變
		
		Container con = getContentPane();
		
		JButton b1 = new JButton("添加");
		JButton b2 = new JButton("刪除");
		JButton b3 = new JButton("查詢");
		JButton b4 = new JButton("修改");
		JButton b5 = new JButton("系統");
		JButton b6 = new JButton("退出");
		
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		
		p1.add(b1);  
		p1.setBorder(new MatteBorder(1,1,1,1,Color.black));
		p1.setBackground(Color.yellow );
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b1) {
					AddS o1 = new AddS();  //添加
					dispose();	
				}			
			}
		});
		
		p2.add(b2);
		p2.setBorder(new MatteBorder(1,1,1,1,Color.black));
		p2.setBackground(Color.yellow );
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b2) {
					Delete d1 = new Delete();  //刪除
					dispose();	
				}			
			}
		});
		
		p3.add(b3);
		p3.setBorder(new MatteBorder(1,1,1,1,Color.black));
		p3.setBackground(Color.yellow );
		b3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b3) {
					Lookup l1 = new Lookup();  //查詢
					dispose();	
				}			
			}
		});
		
		p4.add(b4);
		p4.setBorder(new MatteBorder(1,1,1,1,Color.black));
		p4.setBackground(Color.yellow );
		b4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b4) {
					ChangeS c1 = new ChangeS();  //修改
					dispose();	
				}			
			}
		});
		
		p5.add(b5);
		p5.setBorder(new MatteBorder(1,1,1,1,Color.black));
		p5.setBackground(Color.yellow );
		b5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b5) {
					XiTong x1 = new XiTong();  //系統
					dispose();	
				}			
			}
		});
		
		p6.add(b6);
		p6.setBorder(new MatteBorder(1,1,1,1,Color.black));
		p6.setBackground(Color.yellow );
		b6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b6) {
					System.exit(0);  //退出
					dispose();	
				}			
			}
		});
	
		p7.add(p1);
		p7.add(p2);
		p7.add(p3);
		p7.add(p4);
		p7.add(p5);
		p7.add(p6);
		p7.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 60));
		con.add(p7);
				
	}
}

添加—添加學生

package Student;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;

public class AddS extends JFrame {

	public AddS() {
		
		setTitle("添加");
		setBounds(600, 300, 300, 360);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		
		JTextField t1,t2,t3,t4,t5;
		JLabel l1,l2,l3,l4,l5;
		JButton b1,b2;
		
		Container con = getContentPane();
		
		l1 = new JLabel("學號: ");
		l2 = new JLabel("姓名: ");
		l3 = new JLabel("年齡: ");
		l4 = new JLabel("性別: ");
		l5 = new JLabel("班級: ");
		
		b1 = new JButton("確認");
		b2 = new JButton("取消");
		
		t1 = new JTextField(15);
		t2 = new JTextField(15);
		t3 = new JTextField(15);
		t4 = new JTextField(15);
		t5 = new JTextField(15);
		
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		JPanel p8 = new JPanel();
		
		p1.add(l1);
		p1.add(t1);
		p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p1.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p2.add(l2);
		p2.add(t2);
		p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p2.setBackground(Color.YELLOW); //設置此組件的背景色。
	
		p3.add(l3);
		p3.add(t3);
		p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p3.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p4.add(l4);
		p4.add(t4);
		p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p4.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p5.add(l5);
		p5.add(t5);
		p5.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p5.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p6.add(b1);
		p6.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
		p6.setBackground(Color.YELLOW);
		
		p7.add(b2);
		p7.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
		p7.setBackground(Color.YELLOW);
		
		p8.add(p1);
		p8.add(p2);
		p8.add(p3);
		p8.add(p4);
		p8.add(p5);
		p8.add(p6);
		p8.add(p7);
		p8.setLayout(new FlowLayout(FlowLayout.CENTER,30,15)); //居中,水平距30,垂直距15
		con.add(p8);
		
		b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
				List<Student> list1 = MysqlAll.all1();
				String s1 = t1.getText();
				String s2 = t2.getText();
				String s3 = t3.getText();
				String s4 = t4.getText();
				String s5 = t5.getText();
				Student s = new Student(s1,s2,s3,s4,s5);
				
				for(Student st:list1) {
					if(st.getId().equals(s1)) {
					   JOptionPane.showMessageDialog(null,"學號已存在","錯誤",JOptionPane.ERROR_MESSAGE);
						return; //終止函數的執行,
					}
				}
				
				if( DAO.IfNull(s1) == true || DAO.IfNull(s2) == true ) { //不能空值
					JOptionPane.showMessageDialog(null,"學號或姓名爲空","錯誤",JOptionPane.ERROR_MESSAGE);
				}
				
				else {
				boolean res = DAO.AddInS(s);
					if (res == true) {
						JOptionPane.showMessageDialog(b1,"添加成功");  //消息對話框
						t1.setText("");
						t2.setText("");
						t3.setText("");
						t4.setText("");
						t5.setText("");
							} 
					else {
						JOptionPane.showMessageDialog(null,"添加錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
					}
				}
			}	
		});
		
		b2.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				Operate h1 = new Operate();
				dispose();  //釋放由此 Window、其子組件及其擁有的所有子組件所使用的所有本機屏幕資
			}
		});			
	}
}

添加—添加管理員

package Student;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;

public class AddU extends JFrame {
	
	JTextField t1,t2,t3,t4;
	JLabel l1,l2,l3,l4;
	JButton b1,b2;
	public AddU() {
		Container con = getContentPane();
		
		l1 = new JLabel("用戶: ");
		l2 = new JLabel("密碼: ");
		l3 = new JLabel("性別: ");
		l4 = new JLabel("電話: ");
		
		b1 = new JButton("確認");
		b2 = new JButton("取消");
		
		t1 = new JTextField(15);
		t2 = new JTextField(15);
		t3 = new JTextField(15);
		t4 = new JTextField(15);
	
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		
		p1.add(l1);
		p1.add(t1);
		p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p1.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p2.add(l2);
		p2.add(t2);
		p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p2.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p3.add(l3);
		p3.add(t3);
		p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p3.setBackground(Color.YELLOW); //設置此組件的背景色。

		p4.add(l4);
		p4.add(t4);
		p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p4.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p5.add(b1);
		p5.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
		p5.setBackground(Color.YELLOW);

		
		p6.add(b2);
		p6.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
		p6.setBackground(Color.YELLOW);

		
		p7.add(p1);
		p7.add(p2);
		p7.add(p3);
		p7.add(p4);
		p7.add(p5);
		p7.add(p6);
		p7.setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); //居中,水平距20,垂直距20
		con.add(p7);
		
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				List<User> user1 = MysqlAll.all2();
				
				String u1 = t1.getText();
				String u2 = t2.getText();
				String u3 = t3.getText();
				String u4 = t4.getText();
				User user2 = new User(u1,u2,u3,u4);
				
				for(User user : user1) {
					if(user.getUname().equals(u1)) {
					   JOptionPane.showMessageDialog(null,"用戶已存在","錯誤",JOptionPane.ERROR_MESSAGE);
						return; //終止函數的執行,
					}
				}
				
				if( DAO.IfNull(u1) == true || DAO.IfNull(u2) == true ) { //不能空值
					JOptionPane.showMessageDialog(null,"用戶或密碼不能爲空","錯誤",JOptionPane.ERROR_MESSAGE);
				}
				
				else {
				boolean res = DAO.AddInU(user2);
					if (res == true) {
						JOptionPane.showMessageDialog(b1,"添加成功");  //消息對話框
						t1.setText("");
						t2.setText("");
						t3.setText("");
						t4.setText("");
					} 
					else {
						JOptionPane.showMessageDialog(null,"添加錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
					}
				}
			}	
		});
		
		b2.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				XiTong x1 = new XiTong();
				dispose();	
				}
			});
	}
}

刪除—輸入學號查找

package Student;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;

public class Delete extends JFrame {

	static JTextField t1;
  public Delete () {
	  
	   setTitle("刪除");
	   setBounds(600, 300, 300, 360);
	   setVisible(true);
	   setResizable(false);
	   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	  
	  
	  JLabel l1;
	  JButton b1,b2;
	  t1 = new JTextField(15);
	  l1 = new JLabel("輸入學號:");
	  b1 = new JButton("確認");
	  b2 = new JButton("取消");
	  Container con = getContentPane();
	  
	  JPanel p1 = new JPanel();
	  JPanel p2 = new JPanel();
	  JPanel p3 = new JPanel();
	  JPanel p4 = new JPanel();
	  JPanel p5 = new JPanel();
	  JPanel p6 = new JPanel();
	  JPanel p7 = new JPanel();
	  
	  p1.add(l1);
	  p1.add(t1);
	  p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
	  p1.setBackground(Color.YELLOW); //設置此組件的背景色。
	  p2.add(p1);
	  
	  p3.add(b1);
	  p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
	  p3.setBackground(Color.yellow);
	  p4.add(p3);
	  
	  p5.add(b2);
	  p5.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
	  p5.setBackground(Color.yellow);
	  p6.add(p5);
	  
	  p7.setLayout(new FlowLayout(FlowLayout.CENTER,40,80)); //居中,水平距40垂直距80
	  p7.add(p2);
	  p7.add(p4);
	  p7.add(p6);
	  con.add(p7);  
	  
	  b1.addActionListener(new ActionListener() {
		  public void actionPerformed(ActionEvent e) {
			if(e.getSource() == b1) {
				  if( DAO.LookId(t1.getText()).size() == 0 ) {
						JOptionPane.showMessageDialog(null, "學號不存在","錯誤",JOptionPane.ERROR_MESSAGE);
					}
				  else {
				DeleteShow ds = new DeleteShow();
				dispose();
				  }
			}
		}
	});
	  
	 b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Operate h1 = new Operate();
				dispose();  //釋放由此 Window、其子組件及其擁有的所有子組件所使用的所有本機屏幕資
			}
		});
  }
	
	
}

刪除—是否刪除

package Student;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

public class DeleteShow  extends JFrame{


JLabel l1,l2,l3,l4,l5,l6;
	
public DeleteShow() {
	
	    setTitle("刪除該學生信息");
	    setBounds(800, 200, 350, 580);
        setVisible(true);
        setResizable(false);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	Container con = getContentPane();
	List<Student> list = DAO.LookId(Delete.t1.getText());		
	JButton b1 = new JButton("是");
	JButton b2 = new JButton("否");
	l6 = new JLabel("確定要刪除該學生的信息嗎 ? ");
	for(Student st : list) {
			
		l1 = new JLabel("學號:  "+st.getId());
		l2 = new JLabel("姓名:  "+st.getName());
		l3 = new JLabel("年齡:  "+st.getAge());
		l4 = new JLabel("性別:  "+st.getSex());
		l5 = new JLabel("班級:  "+st.getBanji());
	}
		
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JPanel p3 = new JPanel();
		JPanel p4 = new JPanel();
		JPanel p5 = new JPanel();
		JPanel p6 = new JPanel();
		JPanel p7 = new JPanel();
		JPanel p8 = new JPanel();
		JPanel p9 = new JPanel();
		JPanel p0 = new JPanel();

		p1.add(l1);
		p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p1.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p2.add(l2);
		p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p2.setBackground(Color.YELLOW); //設置此組件的背景色。
	
		p3.add(l3);
		p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p3.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p4.add(l4);
		p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p4.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p5.add(l5);
		p5.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p5.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p6.add(l6);
		p6.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p6.setBackground(Color.YELLOW); //設置此組件的背景色。
		
		p7.add(b1);
		p7.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p7.setBackground(Color.RED); //設置此組件的背景色。
		
		p8.add(b2);
		p8.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //創建具有指定 insets 和顏色的襯邊邊框
		p8.setBackground(Color.white); //設置此組件的背景色。
		
		
		p9.add(p1);
		p9.add(p2);
		p9.add(p3);
		p9.add(p4);
		p9.add(p5);
		p9.add(p6);
		p9.add(p7);
		p9.add(p8);
		p9.setLayout(new FlowLayout(FlowLayout.CENTER,150,30)); //居中,水平距150垂直距30
		add(p9);
		
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if( DAO.DeleteS(Delete.t1.getText()) ) {	
					JOptionPane.showMessageDialog(null, "刪除成功");
					Delete d1 = new Delete();
					dispose();
				}
				else
					JOptionPane.showMessageDialog(null, "刪除失敗","錯誤",JOptionPane.ERROR_MESSAGE);
			}
		});
		
		b2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == b2) {
					dispose();
					Delete d1 = new Delete();
					dispose();
				}
			}
		});
	}
}

連接數據庫

package Student;

import java.sql.*;

public class DataBase {
	
	static Connection con = null;	
	//連接student數據庫
	public static Connection getConnection1(String u,String p) {
		String uri = "jdbc:mysql://localhost:3306/xinxi?useSSL=true&characterEncoding-utf-8";
		try {
			//加載驅動
			Class.forName("com.mysql.jdbc.Driver");
			//獲取連接對象
			con = DriverManager.getConnection(uri,u, p);

		}
		catch (ClassNotFoundException | SQLException e) {
            System.out.println("連接失敗!!!");
			e.printStackTrace();
        }
        return con;
	}
	
	//連接user數據庫
	public static Connection getConnection2(String u,String p) {
		String uri = "jdbc:mysql://localhost:3306/xinxi?useSSL=true&characterEncoding-utf-8";
		try {
			//加載驅動
			Class.forName("com.mysql.jdbc.Driver");
			//獲取連接對象
			con = DriverManager.getConnection(uri,u, p);

		}
		catch (ClassNotFoundException | SQLException e) {
            System.out.println("連接失敗!!!");
			e.printStackTrace();
        }
        return con;
	}
}

源碼下載:

鏈接:https://pan.baidu.com/s/1D4KNMvPzymcDpEOp6-ZdKQ
提取碼:an01

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章