package frame; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; public class ProjectAddDiag extends JDialog implements ActionListener { //定义我需要的swing组件 JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9,jl10; JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7,jf8,jf9,jf10; JPanel jp1,jp2,jp3; JButton jb1,jb2; //owner代笔父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口 public ProjectAddDiag(Frame owner,String title, boolean modal){ //调用父类方法 super(owner,title,modal); jl1 = new JLabel("项目编号"); jl2 = new JLabel("项目名称"); jl3 = new JLabel("负责老师"); jl4 = new JLabel("参与的研究生"); jl5 = new JLabel("项目类型"); jl6 = new JLabel("项目等级"); jl7 = new JLabel("项目经费"); jl8 = new JLabel("项目来源"); jf1 = new JTextField(10); jf2 = new JTextField(10); jf3 = new JTextField(10); jf4 = new JTextField(10); jf5 = new JTextField(10); jf6 = new JTextField(10); jf7 = new JTextField(10); jf8 = new JTextField(10); jb1 = new JButton("添加"); jb1.addActionListener(this); jb2 = new JButton("取消"); jb2.addActionListener(this); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); //设置布局 jp1.setLayout(new GridLayout(10,1)); jp2.setLayout(new GridLayout(10,1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp1.add(jl6); jp1.add(jl7); jp1.add(jl8); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); jp2.add(jf5); jp2.add(jf6); jp2.add(jf7); jp2.add(jf8); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300,520); this.setVisible(true); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource() == jb1){ Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); System.out.println("加载成功"); //2.连接数据库 //定义几个常量 String url = "jdbc:mysql://localhost:3306/keshe?characterEncoding=utf8"; String user = "root"; String passwd = "luoyiluoyi"; ct = DriverManager.getConnection(url,user,passwd); //与编译语句对象 String strsql = "insert into Project values(?,?,?,?,?,?,?,?)"; pstmt = ct.prepareStatement(strsql); //给对象赋值 pstmt.setString(1,jf1.getText()); pstmt.setString(2,jf2.getText()); pstmt.setString(3,jf3.getText()); pstmt.setString(4,jf4.getText()); pstmt.setString(5,jf5.getText()); pstmt.setString(6,jf6.getText()); pstmt.setString(7,jf7.getText()); pstmt.setString(8,jf8.getText()); pstmt.executeUpdate(); this.dispose();//关闭学生对话框 }catch(Exception arg1){ arg1.printStackTrace(); }finally{ try{ if(rs!=null){ rs.close(); rs = null; } if(pstmt != null){ pstmt.close(); pstmt = null; } if(ct != null){ ct.close(); ct = null; } }catch(Exception arg2){ arg2.printStackTrace(); } } } if(e.getSource() == jb2){ this.dispose(); System.out.println(" dsd"); } } }

微信网友_6267404517216256 LV1
2024年5月1日
李朝磊 LV18
2023年11月18日
夜猫子111 LV2
2023年11月6日
fesfefe LV13
2023年9月19日
xiao小果 LV13
2022年5月24日
18692870445 LV1
2021年12月25日
qianzf LV12
2021年12月23日
尹恒yingying LV18
2021年10月12日
13112360 LV2
2021年8月27日
wodemabu88 LV1
2021年8月1日