insertinfo.java

来自「简单的学生管理系统」· Java 代码 · 共 64 行

JAVA
64
字号
package catking.home.love;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JOptionPane;


public class InsertInfo extends EditClientInfo{
	private Connection con;
	public InsertInfo(Connection con,String title,String EditInfo)
	{
		super(title,EditInfo);
		this.con = con;
		
	}
	@Override
	protected void isConfirm() {
		// TODO Auto-generated method stub
		//检测是否有空值
		for(int i =0; i < tf.length; ++i)
		{
			if(tf[i].getText().equals(""))
			{
				JOptionPane.showMessageDialog(null, 
						"输入错误", this.itemNames[i] + "栏不能为空",
							 JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
		//尝试插入操作
		// id 	name	sex		dept
		String[] strInfo = new String[4];
		int age;
		String sqlAppend = "INSERT INTO S(SNO,SN,SEX,DEPT,AGE)VALUES(?,?,?,?,?)";
		try {
			PreparedStatement ps = con.prepareStatement(sqlAppend);
			for(int i =0; i < strInfo.length; ++i)
			{
				strInfo[i] = this.tf[i].getText();
				ps.setString(i + 1 ,strInfo[i]);
			}
			age = Integer.parseInt(this.tf[4].getText());
			ps.setInt(5,age);
			//添加
			ps.executeUpdate();
			JOptionPane.showMessageDialog(null, 
					"添加成功", "您插入记录已经成功了!",
						 JOptionPane.PLAIN_MESSAGE);
			//关闭ps
			ps.close();
			
		} catch (SQLException e) {
			
			JOptionPane.showMessageDialog(null, 
					"输入错误", "对不起,添加数据失败!",
						 JOptionPane.ERROR_MESSAGE);
		}
		
		
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?