Java实现简单个人信息录入

Java实现简单个人信息录入,第1张

数据库就要你自己建了.

import 庆局javax.swing.*

import javax.swing.event.*

import java.awt.*

import java.awt.event.*

import java.sql.*

public class PersonInfoRecorder extends JFrame implements ActionListener{

private JLabel nameLabel,sexLabel,ageLabel,tipLabel//用来稿含显示姓名、性别、年龄和提示栏

private JTextField nameTextField,ageTextField//用来输入姓名和年龄

private JComboBox sex//用来选择性别

private JTextArea info//用来显示输入的个人信息

private JButton ok//确定按钮

private JPanel northPanel,centerPanel,bigPanel

public PersonInfoRecorder(){

super("个人信息录入器")

nameLabel = new JLabel("姓名")

sexLabel = new JLabel("性别")

ageLabel = new JLabel("年龄")

nameTextField = new JTextField(6)

ageTextField = new JTextField(6)

String sexs[] = {"键差笑男","女" }

sex = new JComboBox(sexs)

/*sex.addItemListener(

new ItemListener(){

public void itemStateChanged(ItemEvent event){

if(event.getStateChange() == ItemEvent.SELECTED){

String fsex = (String)sex.getSelectedItem()

info.setText(fsex)

}

}

})*/

ok = new JButton("确定")

ok.addActionListener(this)

northPanel = new JPanel()

northPanel.add(nameLabel)

northPanel.add(nameTextField)

northPanel.add(sexLabel)

northPanel.add(sex)

northPanel.add(ageLabel)

northPanel.add(ageTextField)

northPanel.add(ok)

info = new JTextArea("输入个人简要信息",5,30)

info.setLineWrap(true)

centerPanel = new JPanel()

JScrollPane scroll = new JScrollPane(info)

scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)

scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS)

centerPanel.add(scroll)

bigPanel = new JPanel()

bigPanel.add(northPanel)

bigPanel.add(centerPanel)

getContentPane().add(bigPanel)

setSize(400,200)

setVisible(true)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

public void actionPerformed(ActionEvent e){

if(nameTextField.getText().equals("")){

JOptionPane.showMessageDialog(null, "姓名不能为空!请输入姓名.")

}else{

String inform = info.getText()

if(inform.equals("输入个人简要信息")){

inform = ""

}

info.setText("姓名:" + nameTextField.getText()

+ "\n性别:" + (String)sex.getSelectedItem()

+ "\n年龄:" +

"\n简介:\n" + inform)

try{ //这里的异常处理语句是必需的.否则不能通过编译!

String sqlStr = "insert into Person values(nameTextField.getText(),"+

"(String)sex.getSelectedItem(),ageTextField.getText(),inform)"

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")

String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=Person"

//Person为数据库

String user="sa"

String password=""

Connection con = DriverManager.getConnection(url,user,password)

Statement st = con.createStatement()

st.executeUpdate( sqlStr )

st.close()

con.close()

} catch(ClassNotFoundException exception) {

exception.printStackTrace(System.out)

}

catch(Exception err) {

err.printStackTrace(System.out)

}

}

}

public static void main(String[] args) {

PersonInfoRecorder application = new PersonInfoRecorder()

}

}

这里有一个类

实现学生学号,数学,语文,英语成绩录入

并且计算平均成绩,按照平均成绩亏亮碰高低输出信息

你可以改改!

//实现简单的学生信息输入输出和初步的成绩排序

public class Student {

private int id//学号

private int mathScore//数学成绩

private int chinScore//语文成绩

private int foreScore//外语成绩

public Student() {

id = 0

mathScore = 0

chinScore = 0

foreScore = 0

}

public Student(int newId, int newMathScore, int newChinSvore,

int newForeScore) {

id = newId

mathScore = newMathScore

chinScore = newChinSvore

foreScore = newForeScore

}

public double getAverageScore() { //求平均成绩

double averageScore = ((double) mathScore + chinScore + foreScore) / 3

return averageScore

}

public void output(Student student) { //输出对象的内容

System.out.println(" " + student.id + " " + student.mathScore +

"" + student.chinScore + " "键宴

+ student.foreScore + " " +

student.getAverageScore())

}

public int max(Student a[], int n) { //Student类对象数组的前n项中销谈的成绩最大值的索引

int position = 0

for (int i = 1i <ni++) {

if (a[i].getAverageScore() >a[position].getAverageScore()) { //比较平均成绩

position = i

}

}

return position

}

public void selectSort(Student a[]) { //Student类对象数组的选择排序

for (int n = a.lengthn >1n--) {

int i = max(a, n)

Student temp = a[i]

a[i] = a[n - 1]

a[n - 1] = temp

}

}

}

package test

import java.util.Scanner  // Import the Scanner class

public class 档大肆Test{

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in)  // Create a Scanner object

System.out.print("输入年龄行轿:")

String age= myObj.nextLine()  // Read user input

System.out.print("输入姓名:")

String name= myObj.nextLine()  // Read user input

//再写 地点。。。仿喊

System.out.println(name+" 你好,今年" + age+"岁")  // Output user input

}}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12380943.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存