package view;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import DatabaseConnection.DBconnector;
import VO.course;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import jdk.internal.org.objectweb.asm.commons.SerialVersionUIDAdder;
public class AddCourse1 extends JFrame implements ActionListener{
private static final long SerialVersionUID =1L;
JLabel JL=new JLabel("添加课题",JLabel.CENTER);//主标签
JLabel JLNumber= new JLabel("教师编号:");
JTextField JTNumber= new JTextField();
JLabel JLName=new JLabel("课题名称:");
JTextField JTName=new JTextField();
JLabel JLText=new JLabel("课题内容:");
JTextArea JTText=new JTextArea(3,10);
JButton JB1=new JButton("添加");
JButton JB2=new JButton("重置");
JButton JB3=new JButton("退出");
JLabel img=new JLabel(new ImageIcon("center.jpg"));
public AddCourse1()
{
this.setSize(500,400);
this.setLocationRelativeTo(null);
this.setLayout(null);
JL.setFont(new java.awt.Font("宋体",Font.PLAIN,20));
JL.setBounds(135,30,200,40);
this.add(JL);
JLNumber.setBounds(100,80,100,20);
this.add(JLNumber);
JTNumber.setBounds(200,80,180,20);
this.add(JTNumber);
JLName.setBounds(100,120,100,20);
this.add(JLName);
JTName.setBounds(200,120,180,20);
this.add(JTName);
JLText.setBounds(200, 150,200, 40);
this.add(JLText);
JTText.setBounds(145, 200, 200, 80);
JTText.setLineWrap(true);
this.add(JTText);
JB1.setBounds(80,320,90,20);
this.add(JB1);
JB1.addActionListener(this);
JB2.setBounds(190,320,90,20);
this.add(JB2);
JB2.addActionListener(this);
JB3.setBounds(300,320,90,20);
this.add(JB3);
JB3.addActionListener(this);
img.setBounds(0, 0,500, 400);
this.add(img);
this.setVisible(true);//显示窗口要放在后面,不然图片加载不了。。。
this.setTitle("添加课题");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
String sql="";
public void actionPerformed(ActionEvent e) {
if(e.getSource()==JB1)
{
course c=new course();
c.setCteacherid(Integer.parseInt(JTNumber.getText()));
c.setCname(JTName.getText());
c.setText(JTText.getText());
sql="select * from course where cname= '"+c.getCname()+"'";
//连接数据库
Connection con=DBconnector.getConnection();
//创建Statement对象
try {
Statement stmt=(Statement) con.createStatement();
ResultSet rs= (ResultSet) stmt.executeQuery(sql);
System.out.println("资源获得成功");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"该课题已经添加");
rs.close();
}
else{
sql="insert into course(cteacherid,cname,ctext)"
+ "values("+c.getCteacherid()+",'"+c.getCname()+"','"+
c.getText()+"')";
int i=stmt.executeUpdate(sql);
if(i>0)
{
JOptionPane.showMessageDialog(null,"添加成功");
}
else{
JOptionPane.showMessageDialog(null,"添加失败");
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
else if(e.getSource()==JB2)
{
JTNumber.setText(null);
JTName.setText(null);
JTText.setText(null);
}
else if(e.getSource()==JB3)
{
this.setVisible(false);
}
}
public static void main(String[] args) {
new AddCourse1();
}
}