- 1、用java寫一個程序,用變量保存自己的個人信息,並且輸出
- 2、Java實現簡單個人信息錄入
- 3、編寫一個簡單的java程序顯示個人信息:姓名,性別,出生年月,愛好?
import java.util.Scanner;
public class 個人信息
{
public static void main(String[] args)
{
System.out.println(“\n\t\t==========簡單的個人信息錄入==========\n”);
init();
}//初始化!
private static void init()
{
/*int a1 = 073;
long b1 = 0xa38f;
System.out.println(a1+”…..”+b1);*/
for (; ; )
{
System.out.println(“請輸入姓名:”);
String a=Input.getName();
System.out.println(“請輸入年齡:”);
int b=Input.getAge();
System.out.println(“請輸入學歷:”);
String c=Input.getEducation();
System.out.println(“你輸入的信息^_^\t名字:”+a+” 年齡:”+b+” 學歷:”+c+”\n”);
}
}
}
class Input
{
private static Scanner sc=new Scanner(System.in);
private Input(){}
static String getName()
{
String name=sc.next();
return name;
}
static int getAge()
{
int age=sc.nextInt();
return age;
}
static String getEducation()
{
String education=sc.next();
return education;
}
}
數據庫就要你自己建了.
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:1433;DatabaseName=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 MyInfo {
public static void main(String []args) {
System.out.println(“你好,以下是我的個人信息:”);
System.out.println(“姓名:李剛”);
System.out.println(“性別:女”);
System.out.println(“出生年月:1998-08”);
System.out.println(“愛好:上網”);}
}
原創文章,作者:NOHHJ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/126280.html