IO流+数据库课后习题

1,读取 试题文件 然后做题算分

        File file1=new File("D:\\file","test.txt");
try{
FileReader in1=new FileReader(file1);
BufferedReader in2=new BufferedReader(in1);
String s;
int count=0;
for(;(s=in2.readLine())!=null;){
if(!s.startsWith("-")){
System.out.println(s);
}
else{
System.out.print("input your answer: ");
s=s.replaceAll("-","");
String daan;
Scanner scanner1=new Scanner(System.in);
daan=scanner1.next();
if(s.equals(daan)){
count++;
}
}
}
System.out.println("point "+count);
}
catch(Exception e){
System.out.println(e.getMessage());
}

IO流+数据库课后习题


2,用卡片布局做两个页面,来输入和输出

package testWin;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.CardLayout; public class TestWin implements ActionListener {
//使用卡片式布局,由菜单栏调用两个页面
private JFrame frame;
File file1;
JMenuItem input = new JMenuItem("input");
JMenuItem show = new JMenuItem("show");
InputArea inputMessage;
CardLayout card;
JPanel panel;
JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestWin window = new TestWin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the application.
*/
public TestWin() {
initialize();
} /**
* Initialize the contents of the frame.
*/
private void initialize() {
file1=new File("D:\\file","test.txt");
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar); JMenu menu = new JMenu("\u83DC\u5355");
menuBar.add(menu); menu.add(input); menu.add(show); input.addActionListener(this);
show.addActionListener(this); textArea=new JTextArea(12,50);
inputMessage=new InputArea(file1);
card=new CardLayout();
panel=new JPanel();
panel.setLayout(card);
panel.add("input",inputMessage);
panel.add("show",textArea);
frame.add(panel);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==input){
card.show(panel, "input");
}
else if(e.getSource()==show){
int number=1;
textArea.setText(null);
card.show(panel, "show");
try{
RandomAccessFile in=new RandomAccessFile(file1,"r");
String name=null;
for(;(name=in.readUTF())!=null;){
textArea.append("\n"+"name :"+name);
textArea.append("\t"+in.readUTF());
textArea.append("\t"+in.readUTF());
textArea.append("\n----------------------------------------------------------------------------");
number++;
} in.close();
}
catch(Exception e1){
System.out.println(e1.getMessage());
System.out.println("2222");
}
}
} }
package testWin;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.JLabel; public class InputArea extends JPanel implements ActionListener{ /**
* Create the panel.
*/
File file1;
RandomAccessFile out1;//运用盒式布局,然后用随机流输入到文件
Box baseBox,box1,box2;
JButton button1;
private JLabel lblNewLabel;
private JLabel lblNewLabel_1;
private JLabel lblNewLabel_2;
private JLabel lblNewLabel_3;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JButton button;
public InputArea(File f) {
setForeground(Color.CYAN);
this.file1=f;
baseBox=Box.createHorizontalBox();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
baseBox.add(box1); lblNewLabel = new JLabel("\u8F93\u5165\u59D3\u540D");
box1.add(lblNewLabel); lblNewLabel_1 = new JLabel("\u8F93\u5165qq");
box1.add(lblNewLabel_1); lblNewLabel_2 = new JLabel("\u8F93\u5165\u7535\u8BDD");
box1.add(lblNewLabel_2); lblNewLabel_3 = new JLabel("\u5355\u51FB\u5F55\u5165");
box1.add(lblNewLabel_3);
baseBox.add(box2); text1 = new JTextField();
box2.add(text1);
text1.setColumns(10); text2 = new JTextField();
box2.add(text2);
text2.setColumns(10); text3 = new JTextField();
box2.add(text3);
text3.setColumns(10); button = new JButton("\u5F55\u5165");
box2.add(button);
add(baseBox);
button.addActionListener(this); }
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
try{
RandomAccessFile out1=new RandomAccessFile(file1,"rw");
long length=file1.length();
out1.seek(length);
out1.writeUTF("姓名 : "+text1.getText());
out1.writeUTF("qq : "+text2.getText());
out1.writeUTF("电话 : "+text3.getText());
out1.close();
text1.setText(null);
text2.setText(null);
text3.setText(null);
}
catch(Exception e1){
System.out.println(e1.getMessage());
}
} }

IO流+数据库课后习题

IO流+数据库课后习题

3,编译器(编译可以,链接有bug

主类

package testWin;

import java.awt.BorderLayout;
import java.awt.EventQueue; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*; import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField; public class TestFrame extends JFrame implements ActionListener{ private JPanel contentPane;
private JTextField text1;
private JTextField text2;
JButton button1 = new JButton("\u7528\u8BB0\u4E8B\u672C\u7F16\u8F91\u6E90\u6587\u4EF6");
JButton button2 = new JButton("\u7F16\u8BD1");
JButton button3 = new JButton("\u8FD0\u884C");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 869, 111);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); contentPane.add(button1); JLabel label = new JLabel(" \u8F93\u5165\u6587\u4EF6\u540D \uFF1A");
contentPane.add(label); text1 = new JTextField();
contentPane.add(text1);
text1.setColumns(15); contentPane.add(button2); JLabel label_1 = new JLabel(" \u8F93\u5165\u4E3B\u7C7B\u540D\uFF1A");
contentPane.add(label_1); text2 = new JTextField();
contentPane.add(text2);
text2.setColumns(13); contentPane.add(button3);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
} @Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(e.getSource()==button1){
System.out.println("dd");
Runtime ce=Runtime.getRuntime();
File file1=new File("D:\\javaWorkSpace\\Test\\src\\test22","test2.java");
// File file1=new File("D:\\file","test.java");
try{
ce.exec(file1.getAbsolutePath());
}
catch(Exception e1){
System.out.println(e1.getMessage());
}
}
else if(e.getSource()==button2){
CompileDialog compileDialog=new CompileDialog();
String name=text1.getText();
compileDialog.compile(name);
compileDialog.setVisible(true);
}
else if(e.getSource()==button3){
RunDialog runDialog=new RunDialog();
String name=text2.getText();
runDialog.run(name);
runDialog.setVisible(true);
}
} }

编译类

package testWin;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea; public class CompileDialog extends JDialog { private final JPanel contentPanel = new JPanel();
JTextArea textArea = new JTextArea(14,70);
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
CompileDialog dialog = new CompileDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the dialog.
*/
public CompileDialog() {
setBounds(100, 100, 871, 502);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
scrollPane.setViewportView(textArea);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
} public void compile(String name){
try{
Runtime ce=Runtime.getRuntime();
Process proccess =ce.exec("javac "+name);
InputStream in1=proccess.getErrorStream();
BufferedInputStream in2=new BufferedInputStream(in1);
int n;
boolean bn=true;
byte error[]=new byte[100];
for(;(n=in2.read(error,0,100))!=-1;){
String s=null;
s=new String(error,0,n);
textArea.append(s);
if(s!=null)bn=false;
}
if(bn)
textArea.append("编译正确");
}
catch(IOException e2){
System.out.println(e2.getMessage());
}
}
}

运行类

package testWin;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import javax.swing.JScrollPane;
import javax.swing.JTextArea; public class RunDialog extends JDialog { private final JPanel contentPanel = new JPanel();
JTextArea textArea = new JTextArea(14,20);
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
RunDialog dialog = new RunDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the dialog.
*/
public RunDialog() {
setBounds(100, 100, 686, 448);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane);
{
scrollPane.setViewportView(textArea);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
public void run(String name){
try{
Runtime ce1=Runtime.getRuntime();
Process proccess=ce1.exec("java "+name);
InputStream in1=proccess.getInputStream();
BufferedInputStream in2=new BufferedInputStream(in1);
byte a[]=new byte[100];
int n;
for(;(n=in2.read(a,0,100))!=-1;){
String s=new String(a,0,n);
textArea.append(s);
if(s==null)
System.out.println("333");
}
System.out.println(n);
System.out.println("444");
}
catch(Exception e){
System.out.println(e.getMessage());
}
} }

IO流+数据库课后习题


4,和第一个一样,读取数据库来答题,

import java.io.*;
import java.util.Scanner;
import java.sql.*; public class Test {
public static void main(String args[]){
ReadTest test1=new ReadTest();
test1.setDatabase("Kooing");
test1.setTable("test");
int number=test1.getNumber();
System.out.println("There are "+number+" question what are you want to answer");
Scanner scanner1=new Scanner(System.in);
for(;scanner1.hasNextInt();){
int num=scanner1.nextInt();
String huiche=scanner1.nextLine();
test1.setQusetion(num);
String s=test1.getQusetion();
System.out.println(s);
System.out.println("input your answer :");
String s2=scanner1.nextLine();
// String huiche=scanner1.nextLine();
if(s2.equals(test1.getAnswer().trim())){
System.out.println("right");
}
else{
System.out.println("error");
}
System.out.println("There are "+number+" question what are you want to answer");
}
}
} class ReadTest{
private Connection con;
private Statement sql;
private ResultSet rs;
private String database,table,daan,wenti;
private int question;
ReadTest(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(Exception e){
System.out.println(e.getMessage());
System.out.println("1111");
}
}
void setDatabase(String a){
database=a;
try{
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName="+database,"sa","kenn5666225");
}
catch(Exception e2){
System.out.println(e2.getMessage());
System.out.println("2222222222");
}
}
void setTable(String a){
table=a;
try{
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select *from "+table);
}
catch(Exception e3){
System.out.println(e3.getMessage());
System.out.println("33333333");
}
}
int getNumber(){
int n=0;
try{
rs.last();
n=rs.getRow();
}
catch(Exception e4){
System.out.println(e4.getMessage());
System.out.println("444444444");
}
return n;
}
void setQusetion(int a){
question=a;
}
String getQusetion(){
try{
rs.absolute(question);
wenti=rs.getString(1)+"\n"+rs.getString(2)+"\n"+rs.getString(3)+"\n"+rs.getString(4)+"\n"+rs.getString(5);
}
catch(Exception e5){
System.out.println(e5.getMessage());
System.out.println("55555555");
}
return wenti;
}
String getAnswer(){
try{
daan=rs.getString(6);
}
catch(Exception e6){
System.out.println(e6.getMessage());
System.out.println("6666666666");
}
return daan;
}
}

IO流+数据库课后习题

上一篇:IoC容器Autofac - Autofac + Asp.net MVC + EF Code First(转载)


下一篇:WebService入门学习二