package action;
import java.sql.*;
public class connect {
String drive="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc.microsoft.sqlserver://127.0.0.1:1433;Database=Personal_Management_System";
public Connection con;
public void conn(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
System.out.println("驱动没有找到");
}
//ClassNotFoundException表示如果Class类没有找到,就抛出异常。
try{
con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Personal_Management_System","sa","root");
}catch(SQLException e){
System.out.println("链接错误");
}
}
}
package action;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class input {
public static void add1(String 员工ID,String 姓名,String 性别,String 年龄,String 所在部门,String 岗位ID,String 在职状态) throws SQLException{
// TODO Auto-generated method stub
connect conn=new connect();
conn.conn();
String sql = "insert into 员工基本信息表 values(?,?,?,?,?,?,?) ";
PreparedStatement pst = conn.con.prepareStatement(sql);
pst.setString(1,员工ID);
pst.setString(2,姓名);
pst.setString(3,性别);
pst.setString(4,年龄);
pst.setString(5,所在部门);
pst.setString(6,岗位ID);
pst.setString(7,在职状态);
pst.executeUpdate();
pst.close();
}
public static void add2(String 员工ID,String 婚姻状况,String 配偶姓名,String 工作单位,String 联系方式) throws SQLException{
// TODO Auto-generated method stub
connect conn=new connect();
conn.conn();
String sql = "insert into 员工婚姻情况表 values(?,?,?,?,?) ";
PreparedStatement pst = conn.con.prepareStatement(sql);
pst.setString(1,员工ID);
pst.setString(2,婚姻状况);
pst.setString(3,配偶姓名);
pst.setString(4,工作单位);
pst.setString(5,联系方式);
pst.executeUpdate();
pst.close();
}
public static void add3(String 员工ID,String 学历,String 学位,String 专业,String 毕业时间,String 毕业学校,String 外语等级) throws SQLException{
// TODO Auto-generated method stub
connect conn=new connect();
conn.conn();
String sql = "insert into 员工学历信息表 values(?,?,?,?,?,?,?) ";
PreparedStatement pst = conn.con.prepareStatement(sql);
pst.setString(1,员工ID);
pst.setString(2,学历);
pst.setString(3,学位);
pst.setString(4,专业);
pst.setString(5,毕业时间);
pst.setString(6,毕业学校);
pst.setString(7,外语等级);
pst.executeUpdate();
pst.close();
}
public static void add4(String 员工ID,String 部门名称,String 办公室) throws SQLException{
// TODO Auto-generated method stub
connect conn=new connect();
conn.conn();
String sql = "insert into 企业部门信息表 values(?,?,?) ";
PreparedStatement pst = conn.con.prepareStatement(sql);
pst.setString(1,员工ID);
pst.setString(2,部门名称);
pst.setString(3,办公室);
pst.executeUpdate();
pst.close();
}
}
package inteface;
import java.awt.*;//java.awt包含Swing容器
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;//javax.swing包含了Swing所有组件
//java是指java的扩韩包
import action.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class imformation_input extends JFrame{
//要实现界面,一定要将当前类继承JFrame类
//顶级容器通过继承实现
static JFrame mainFrame=new JFrame();//顶级容器需要实例化,JFrame表示窗口
static JPanel pnl=new JPanel();//次级容器也要实例化
static GridLayout gri=new GridLayout(11,2);//网格布局实例化
//4表示行,2表示列
static JLabel lbl1=new JLabel("姓名:");
static JLabel lbl2=new JLabel("员工ID:");
static JLabel lbl3=new JLabel("性别:");
static JLabel lbl4=new JLabel("年龄:");
static JLabel lbl5=new JLabel("学历:");
static JLabel lbl6=new JLabel("婚姻状况:");
static JLabel lbl7=new JLabel("所在部门:");
static JLabel lbl8=new JLabel("岗位ID:");
static JLabel lbl9=new JLabel("配偶姓名:");
static JLabel lbl10=new JLabel("在职状态:");
static JTextField tf1=new JTextField();
static JTextField tf2=new JTextField();
static JTextField tf3=new JTextField();
static JTextField tf4=new JTextField();
static JTextField tf5=new JTextField();
static JTextField tf6=new JTextField();
static JTextField tf7=new JTextField();
static JTextField tf8=new JTextField();
static JTextField tf9=new JTextField();
static JTextField tf10=new JTextField();
static JButton btn1=new JButton("重置");
public static JButton btn2=new JButton("添加");
public static String 姓名=new String();
public static String 员工ID=new String();
public static String 性别=new String();
public static String 学历=new String();
public static String 婚姻情况=new String();
public static String 所在部门=new String();
public static String 年龄=new String();
public static String 岗位ID=new String();
public static String 配偶姓名=new String();
public static String 在职状态=new String();
static main_interface mi=new main_interface();
static input in=new input();
/**
* @param args
*/
public static void add() {
// TODO Auto-generated method stub
mainFrame.setSize(400, 300);//setSize设置窗口大小,400表示宽,300表示高,单位是像素
mainFrame.setTitle("员工信息输入");//setTitle设置窗口的标题
mainFrame.setResizable(false);//setResiable表示不允许用户调整窗口大小
mainFrame.setLocation(200, 150);
//setLocation设置窗口初始化的时候,在屏幕中的相对位置。以屏幕左上角为原点进行坐标,单位是像素
//200表示x轴的位置,150表示y轴的位置
Container conta=mainFrame.getContentPane();
//返回窗体的content对象,包含次级容器
pnl.setSize(300, 200);//pnl表示次级容器对象,设置次级容器的大小
pnl.setBackground(Color.white);//setBackground表示设置背景颜色
pnl.setLayout(gri);//setLayout表示使用网格布局
pnl.add(lbl1);//add表示添加组件
pnl.add(tf1);
pnl.add(lbl2);
pnl.add(tf2);
pnl.add(lbl3);
pnl.add(tf3);
pnl.add(lbl4);
pnl.add(tf4);
pnl.add(lbl5);
pnl.add(tf5);
pnl.add(lbl6);
pnl.add(tf6);
pnl.add(lbl7);
pnl.add(tf7);
pnl.add(lbl8);
pnl.add(tf8);
pnl.add(lbl9);
pnl.add(tf9);
pnl.add(lbl10);
pnl.add(tf10);
pnl.add(btn1);
pnl.add(btn2);
conta.add(pnl);//Contain对象是窗体用来加载次级容器的
mainFrame.setVisible(true);
//所有元素和次级容器加载到窗体中以后,必须使用setVisible使窗体可见,否则窗体看不见的。
//获取文本框内容
姓名 = tf1.getText();
员工ID = tf2.getText();
性别 = tf3.getText();
年龄 = tf4.getText();
学历 = tf5.getText();
婚姻情况 = tf6.getText();
所在部门 = tf7.getText();
岗位ID = tf8.getText();
配偶姓名 = tf9.getText();
在职状态 = tf10.getText();
//类型转换
btn2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent l) {
// TODO Auto-generated method stub
main_interface.face();
try {
input.add1(员工ID,姓名, 性别, 年龄, 所在部门, 岗位ID, 在职状态);
input.add2( 员工ID, null, 配偶姓名, null, null);
input.add3(员工ID,学历,null,null,null,null,null);
input.add4(岗位ID,null,null);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mainFrame.setVisible(true);
}
});
//重置按钮
btn1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==btn1){
//arg0表示作用在按钮上的事件,arg0是ActionEvent,表示arg0形参用于监听的
//getSource方法表示获取数据源,事件来自于哪个组件
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
tf8.setText("");
tf9.setText("");
tf10.setText("");
}
}
});
}
}
为什么会导致插入的数据全部显示空的