需要的工具
1.SQL Server
2.Eclipse
3.JDBC连接数据库驱动
https://download.microsoft.com/download/A/F/B/AFB381FF-7037-46CE-AF9B-6B1875EA81D7/sqljdbc_6.0.8112.200_chs.exe

功能实现
分为三大类
1.land.java (登陆界面)
2.system.java (系统管理界面)
3.sql.java (数据库操作,包括连接、断开、增删改查等操作)

一、登陆界面及代码
在这里插入图片描述

部分代码:

登陆按钮事件

public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub
		if((JButton)e.getSource()== buttonLand)
		{
		
		String name = textName.getText().trim();
		String password = String.valueOf(textPassword.getPassword()).trim();
	    int num = sq.landing(name, password);
	    if(num==1)
	    {
	    	JOptionPane.showMessageDialog(frame, "欢迎进入学生宿舍管理系统!","提示:",JOptionPane.PLAIN_MESSAGE);
	    	system system = new system();
	    	frame.dispose();
	    }
	    else
	    {
	    	JOptionPane.showMessageDialog(frame, "账号或者密码错误!","提示:",JOptionPane.ERROR_MESSAGE);
	    }
		}
	}

二、系统管理界面及代码**(这里只添加了俩个表,多少表都是一样的道理**)

增加数据:点击增加按钮会出现一空白行,填写完点击保存即可
保存(更新)数据:直接在表中进行相应的操作,再点击保存即可
删除数据:选择某行,点击删除按钮即可

在这里点插入图片描述在这里插入图片描述
部分代码:

//--------------------------------------按钮事件--------------------------------
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//------------------------------- 删除-----------------------------
		String tableName = null;
		String key1 = null;
		if((JButton)e.getSource()==buttonDelete)
		{   if(paneParent.getSelectedIndex()==1)
			{
				tableName = "学生信息表";
				key1 = "sno";
			}
			if(paneParent.getSelectedIndex()==2)
			{
				tableName = "宿舍信息表";
				key1 = "dno";
			}
			int row = table.getSelectedRow();			
			if(row!=-1)
			{
				String key2 = (String) tableModel.getValueAt(row, 0);
				int result = JOptionPane.showConfirmDialog(null, "确定要删除吗?","请确认",JOptionPane.YES_NO_OPTION);
				if(result==JOptionPane.OK_OPTION)
				{
					String sql = "delete from "+tableName+" where "+key1+"="+key2;
					int num = sq.delete(sql);
					if(num>0)
					{
						tableModel.removeRow(row);
					}
				}
			}
			else 
			{	
				JOptionPane.showMessageDialog(null, "请选择要删除的行!","提示:",JOptionPane.ERROR_MESSAGE);
			}
		}
		//------------------------------保存------------------------
		if((JButton)e.getSource()==buttonSave)
		{
			int result = JOptionPane.showConfirmDialog(null, "请确认数值已经更改,否则保存无效","请确认",JOptionPane.YES_NO_OPTION);
			if(result==JOptionPane.OK_OPTION)
			{
				int row = table.getRowCount();
				int column = table.getColumnCount();			
				String[][]valueRow= new String[row][column];
				String[] sqlvalue  = new String[row];
				for(int i = 0; i < row; i++)
				{
					for(int j = 0; j < column; j++)
					{
						valueRow[i][j] = table.getValueAt(i, j).toString();
					}
				}
				if(paneParent.getSelectedIndex()==1)
				{	
					for(int i =0;i<row;i++)
					{
						String sql = "insert into 学生信息表"+" values ("+valueRow[i][0].toString()+","+valueRow[i][1].toString()+","+valueRow[i][2].toString()+","+valueRow[i][3].toString()+","+valueRow[i][4].toString()+","+valueRow[i][5].toString()+")";				
						sqlvalue[i]=sql.toString();									
					}
					data = sq.Save(sqlvalue,"学生信息表",row, column);
		            tableModel.setDataVector(data,dataTitle);			
				}
				if(paneParent.getSelectedIndex()==2)
				{
					for(int i =0;i<row;i++)
					{
						String sql = "insert into 宿舍信息表"+" values ("+valueRow[i][0].toString()+","+valueRow[i][1].toString()+")";				
						sqlvalue[i]=sql.toString();									
					}
					data = sq.Save(sqlvalue,"宿舍信息表",row, column);
		            tableModel.setDataVector(data,TitleDormitor);			
				}				
			}					        
		}
		//------------------------------增加---------------------------
		if((JButton)e.getSource()==buttonIncrease)
		{
			tableModel.addRow(new Vector<>());
		}
	}

三、sql.java (重要)

public class SQL {
    public  Connection conn;
    public  Statement st;
    public  ResultSet rs;
  
//----------------------------获取链接--------------------------------
public Connection getConn() throws SQLException, ClassNotFoundException
{
  String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
  String url = "jdbc:sqlserver://localhost:1433;databaseName=学习宿舍";
//:1433为数据库默认端口号,学习宿舍为数据库名字
  String user = "sa"; //登录用户名
  String password = "123456"; //登录密码
  try { 
   Class.forName(driverClassName);
   conn = DriverManager.getConnection(url, user, password);
   System.out.println("数据库连接成功");
  } catch (SQLException ex1)
  {
   System.out.println("数据库连接失败");
  } 
  return conn;
}

//-----------------------------------关闭链接----------------------------------

public  void Close() 
{
  try {
	rs.close();
} catch (SQLException e) {
	e.printStackTrace();
}
  try {
	st.close();
} catch (SQLException e1) {
	e1.printStackTrace();
}
  try {
	conn.close();
} catch (SQLException e) {
	e.printStackTrace();
}
}
//----------------------------------------登陆---------------------------------
public int landing(String name1,String password1) 
{
	int num = 0;
	String sql = "select *from 用户表";
	try{
		getConn();
		st = conn.createStatement();
		rs = st.executeQuery(sql);
		while(rs.next())
		{
			String name = rs.getString(1).trim();
			String password = rs.getString(2).trim();
			if(name.equals(name1)&&password.equals(password1))
			{
				num = 1;
			}
			
		}
	}catch (SQLException e) {
		// TODO: handle exception
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	Close();
	return num;
}
//--------------------------------查询-------------------------------
public Vector<Vector<Object>> query(String tableName,int column)
{
	int num = 0;
	String sql = "select *from "+tableName;
	Vector<Vector<Object>> data = new Vector<Vector<Object>>();
	try{
		getConn();
		st = conn.createStatement();
		rs=st.executeQuery(sql);
		while(rs.next())
		{
			Vector<Object> rowdata = new Vector<Object>();
			for(num=1;num<=column;num++)
			{
				rowdata.add(rs.getString(num));	  
			}
			data.add(rowdata);
		}
	  }catch(SQLException ex1)
		{
		System.out.println("失败"+ex1);
		} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		Close();
		return data;
}
//---------------------------------------删除----------------------------------
public int delete(String sql)
{
	int num = 0;
	try{
		getConn();
		st = conn.createStatement();
	    num = st.executeUpdate(sql);
	}catch (SQLException e) {
		// TODO: handle exception
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	Close();
	return num;
}
//-------------------------------------保存-------------------------------------
public Vector<Vector<Object>> Save(String[] sqlvalue,String tableName,int row,int column) 
{
	Vector<Vector<Object>> data = new Vector<Vector<Object>>();
	try{
		getConn();
		st = conn.createStatement();
	    st.executeUpdate("delete from "+tableName);
		for(int i =0;i<row;i++)
		{
			st.executeUpdate(sqlvalue[i].toString());
		}		
		data = query(tableName, column);
		
	}catch (SQLException e) {
		// TODO: handle exception
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return data;
}
}

数据库连接账号密码及表名

**

这个是数据库的连接账号和密码123456
这个是数据库的连接账号和密码

这个是用户表结构和登陆账号和密码(可自行修改)
在这里插入图片描述

在这里插入图片描述

**

宿舍信息表结构**
在这里插入图片描述

**

学生信息表结构**
在这里插入图片描述

没想到找我代码的兄弟姐妹有些多,邮箱会漏,大家关注公众号(热心市民鲨鱼先生),里面自提吧
关注下点个按钮就行了。
在这里插入图片描述

Logo

更多推荐