数据库是提前建立好的SCT数据库;
分成Frame.java 和Main.java,SCT.java
开工!
Frame.java
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
*/
/**
* @author 作者:拔牙不打麻药
* @version 创建时间:2020年4月28日 下午10:51:44
*/
/**
* @author 拔牙不打麻药
*
* @time 2020年4月28日
*/
public class Frame extends JFrame{
String s;
Connection conn;
final String URL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCT";
final String USER="sa";
final String PASSWORD="123456";
Statement st;
ResultSet rs;
//提示框
JLabel jl1=new JLabel("请输入要查询的学生姓名和成绩: ");
JLabel jl2=new JLabel("请输入要查询的教师姓名并显示工资: ");
JLabel jl3=new JLabel("请输入要查询的部门并显示部门的所有老师: ");
JLabel jl4=new JLabel("请输入要删除的部门信息: ");
JLabel jl5=new JLabel("请输入要插入的部门信息: ");
JLabel jl6=new JLabel("请输入要更新的学生信息: ");
//文本框
JTextField jt1=new JTextField(20);
JTextField jt2=new JTextField(20);
JTextField jt3=new JTextField(20);
JTextField jt4=new JTextField(20);
JTextField jt5=new JTextField(20);
JTextField jt6=new JTextField(20);
// JTextField jt7=new JTextField(20);
//按钮
JButton jb1=new JButton("查询");
JButton jb2=new JButton("查询");
JButton jb3=new JButton("查询");
JButton jb4=new JButton("删除");
JButton jb5=new JButton("插入");
JButton jb6=new JButton("更新");
//显示框
JTextArea jt=new JTextArea("jt");
public void createframe() {
this.setTitle("教务信息管理模块");
this.setLayout(null);
this.setLocation(300,100);
this.setSize(800, 700);
this.setLayout(null);
//容器
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JPanel jp5=new JPanel();
JPanel jp6=new JPanel();
JPanel jp7=new JPanel();
//设置容器大小
jp1.setBounds(10, 10, 700, 60);
jp2.setBounds(10, 80, 700, 60);
jp3.setBounds(10, 150, 700, 60);
jp4.setBounds(10, 220, 700, 60);
jp5.setBounds(10, 290, 700, 60);
jp6.setBounds(10, 360, 700, 60);
jp7.setBounds(10, 400, 700, 160);
//左对齐
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
jp2.setLayout(new FlowLayout(FlowLayout.LEFT));
jp3.setLayout(new FlowLayout(FlowLayout.LEFT));
jp4.setLayout(new FlowLayout(FlowLayout.LEFT));
jp5.setLayout(new FlowLayout(FlowLayout.LEFT));
jp6.setLayout(new FlowLayout(FlowLayout.LEFT));
jp7.setLayout(new FlowLayout(FlowLayout.LEFT));
//加入面板中
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
jp1.add(jl1);
jp1.add(jt1);
jp1.add(jb1);
jp2.add(jl2);
jp2.add(jt2);
jp2.add(jb2);
jp3.add(jl3);
jp3.add(jt3);
jp3.add(jb3);
jp4.add(jl4);
jp4.add(jt4);
jp4.add(jb4);
jp5.add(jl5);
jp5.add(jt5);
jp5.add(jb5);
jp6.add(jl6);
jp6.add(jt6);
jp6.add(jb6);
jp7.add(jt);
this.setVisible(true);
//设置textarea的字体格式
jt.setFont(new Font("黑体",Font.BOLD,10));
//添加监听事件
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// Method m=new Method();
// m.actionPerformed(e);
if(e.getSource()==jb1) {
if(jt1.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Big胆!居然不输入内容!","狗狗提醒您:",JOptionPane.ERROR_MESSAGE);
jt1.grabFocus();
}
else {
JOptionPane.showMessageDialog(null, "查询成功!建议北大青鸟录取!","古德!",JOptionPane.INFORMATION_MESSAGE);
s=jt1.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
String sql="select S1.Sno,S2.Cno,S2.Score From Student S1,SC S2 Where S1.Sno=S2.Sno and S1.Sname='"+s+"'"+"\n";
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()) {
jt.setText("该同学的学号为"+rs.getString("Sno")+",课程:"+rs.getString("Cno")+",这科成绩为:"+rs.getString("Score")+"\n");
}
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败"+e1.getMessage());
}
}
}
jt1.setText("");
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb2) {
if(jt2.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Big胆!居然不输入内容!","狗狗提醒您:",JOptionPane.ERROR_MESSAGE);
jt2.grabFocus();
}
else {
JOptionPane.showMessageDialog(null, "查询成功!建议北大录取!","古德!",JOptionPane.INFORMATION_MESSAGE);
s=jt2.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
String sql="select t.Tname,c.Cname,c.Cno,t.Salary from Teacher t,Course c where t.Tname='"+s+"' and t.Tno=c.Tno";
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()) {
jt.setText("老师的姓名为"+rs.getString("Tname")+",教授的课程为:"+rs.getString("Cname")+",课程代码为:"+rs.getString("Cno")+",工资为:"+rs.getString("Salary")+'\n');
}
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败"+e1.getMessage());
}
jt2.setText("");
}
}
}
});
jb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb3) {
if(jt3.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Big胆!居然不输入内容!","狗狗提醒您:",JOptionPane.ERROR_MESSAGE);
jt3.grabFocus();
}
else {
JOptionPane.showMessageDialog(null, "查询成功!建议北大录取!","古德!",JOptionPane.INFORMATION_MESSAGE);
s=jt3.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
String sql="select d.Dname, t.Tname from Dept d,Teacher t where d.Dname='"+s+"' and d.Dno=t.Dno;";
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()) {
jt.setText("该部门为"+rs.getString("Dname")+",老师名为:"+rs.getString("Tname")+'\n');
}
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败"+e1.getMessage());
}
jt3.setText("");
}
}
}
});
jb4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb4) {
if(jt4.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Big胆!居然不输入内容!","狗狗提醒您:",JOptionPane.ERROR_MESSAGE);
jt4.grabFocus();
}
else {
JOptionPane.showMessageDialog(null, "查询成功!建议北大录取!","古德!",JOptionPane.INFORMATION_MESSAGE);
s=jt4.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
//String sql="delete from Dept where Dept.Dname='"+s+"';";
String sql="select * from Dept where Dname='"+s+"'";
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()) {
jt.setText("成功删除"+rs.getString("Dname")+"部门,部门编号为" +rs.getString("Dno")+"部门负责人为 "+rs.getString("Dean")+"\n");
}
sql="delete from Dept where Dept.Dname='"+s+"';";
rs=st.executeQuery(sql);
}
catch(Exception e1) {
//JOptionPane.showMessageDialog(null, "数据库连接失败"+e1.getMessage());
}
jt4.setText("");
}
}
}
});
jb5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb5) {
if(jt5.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Big胆!居然不输入内容!","狗狗提醒您:",JOptionPane.ERROR_MESSAGE);
jt5.grabFocus();
}
else {
JOptionPane.showMessageDialog(null, "查询成功!建议北大录取!","古德!",JOptionPane.INFORMATION_MESSAGE);
s=jt5.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
String sql="insert into Dept values('01','"+s+"','周志光');";
jt.setText("成功插入"+s+"部门!\n");
String sql1="select * from Dept where Dname='"+s+"'";
st=conn.createStatement();
rs=st.executeQuery(sql);
Statement st1=conn.createStatement();
ResultSet rs1=st1.executeQuery(sql1);
if(rs1.next()) {
//jt.setText("成功插入"+rs.getString("Dname")+"部门,部门编号为" +rs.getString("Dno")+"部门负责人为 "+rs.getString("Dean")+"\n");
}
}
catch(SQLException e1) {
//JOptionPane.showMessageDialog(null, "数据库连接失败"+e1.getMessage());
}
catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jt5.setText("");
}
}
}
});
jb6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb6) {
if(jt6.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Big胆!居然不输入内容!","狗狗提醒您:",JOptionPane.ERROR_MESSAGE);
jt6.grabFocus();
}
else {
JOptionPane.showMessageDialog(null, "查询成功!建议北大录取!","古德!",JOptionPane.INFORMATION_MESSAGE);
s=jt6.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(URL,USER,PASSWORD);
String sql="update SC set SC.Score=SC.Score*0.9 where SC.Sno=(select s.Sno from Student s where s.Sname='"+s+"')";
jt.setText("更新"+s+"同学的成绩成功!"+'\n');
st=conn.createStatement();
rs=st.executeQuery(sql);
}
catch(Exception e1) {
JOptionPane.showMessageDialog(null, "数据库连接失败"+e1.getMessage());
}
jt6.setText("");
}
}
}
});
}
}
Main.java
/**
*
*/
/**
* @author 作者:拔牙不打麻药
* @version 创建时间:2020年4月29日 下午9:45:23
*/
/**
* @author 拔牙不打麻药
*
* @time 2020年4月29日
*/
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SCT sct=new SCT();
Frame f=new Frame();
sct.getConn();
f.createframe();
}
}
SCT.java
import java.sql.*;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javafx.application.Application;
/**
* @author 作者:拔牙不打麻药
* @version 创建时间:2020年4月26日 下午4:55:55
*/
public class SCT {
private static SCT sct=new SCT();
public SCT() {
try {
//加载驱动程序
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
System.out.println("驱动程序已加载");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "数据库加载失败"+e.getMessage());
}
}
public static Connection getConn() {
try{
final String URL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCT";
final String USER="sa";
final String PASSWORD="123456";
Connection conn=null;
//获得数据库的连接
conn=DriverManager.getConnection(URL,USER,PASSWORD);
System.out.println("已成功连接到数据库");
return conn;
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "数据库连接失败"+e.getMessage());
return null;
}
}
}
好像还有几个bug,但是能用嘻嘻
竣工!