第一题: 图形界面,输入文本地址,在TextFiled里显示内容,点击转换按钮,将大写字母转为小写字母,小写字母转为大写字母并输出在另外一个TextFiled里
import java.io.File; import java.io.FileInputStream; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; public class Task1 { protected Shell shell; private Text textPath; private Text textRecord; private Text textTran; /** * Launch the application. * @param args */ public static void main(String[] args) { try { Task1 window = new Task1(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * Create contents of the window. */ protected void createContents() { shell = new Shell(); shell.setSize(450, 300); shell.setText("\u6587\u672C\u7F16\u8F91\u5668"); Label label = new Label(shell, SWT.NONE); label.setBounds(32, 36, 63, 13); label.setText("\u6587\u4EF6\u8DEF\u5F84\uFF1A"); textPath = new Text(shell, SWT.BORDER); textPath.setBounds(121, 36, 266, 19); Button buttonRead = new Button(shell, SWT.NONE); buttonRead.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { String path = textPath.getText(); byte bytes[]= new byte[1024]; int index=0; try{ File file = new File(path); if(!file.exists()) { textRecord.setText("文本不存在"); } else{ FileInputStream is = new FileInputStream(file); while((index = is.read(bytes,0,1024))>0){ String str = new String(bytes,0,index); textRecord.setText(str); } } } catch(Exception ee){ee.printStackTrace();} } }); buttonRead.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { } }); buttonRead.setBounds(71, 71, 82, 23); buttonRead.setText("\u8BFB\u5165"); Button buttonTrans = new Button(shell, SWT.NONE); buttonTrans.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { try{ if(textRecord.getText()==null){ textTran.setText("文本域为空"); } else{ String text=null; byte bytes[] = new byte[1024]; char chs[]= new char[1024]; bytes = textRecord.getText().toString().getBytes(); chs = textRecord.getTextChars(); int len = textRecord.getCharCount(); int index =0; while(index<len){ if(chs[index]<=122&&chs[index]>=97) { chs[index] = (char)((int)chs[index]-32); } else if(chs[index]>=65&&chs[index]<=90) {chs[index] = (char)((int)chs[index]+32);} index++; } textTran.setText(String.valueOf(chs)); } }catch(Exception ee){ee.printStackTrace();} } }); buttonTrans.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { } }); buttonTrans.setBounds(248, 71, 82, 23); buttonTrans.setText("\u8F6C\u6362"); textRecord = new Text(shell, SWT.BORDER); textRecord.setBounds(34, 100, 166, 137); textTran = new Text(shell, SWT.BORDER); textTran.setBounds(238, 100, 166, 137); } }
第二题,套接字,客户端输入一个算术式:2+3=,服务器端算出结果,返回到客户端
//客户端 import java.io.*; import java.net.*; public class Task2Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ System.out.println("开始连接服务器"); Socket socket = new Socket("localhost", 8888); System.out.println("已经连接服务器"); OutputStream os= socket.getOutputStream(); InputStream is = socket.getInputStream(); byte bytes[] = new byte[3]; //InputStreamReader isr = new InputStreamReader(System.in); char chs[] = new char[50]; int index =0; System.in.read(bytes); os.write(bytes); System.out.println("已经发送"); InputStreamReader isr = new InputStreamReader(is); index=isr.read(chs); System.out.println(new String(chs,0,index)); }catch(Exception e){e.printStackTrace();} } } //服务器端 import java.io.*; import java.net.*; public class Task2Server { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ ServerSocket ss = new ServerSocket(8888); System.out.println("服务器开启"); Socket socket = ss.accept(); System.out.println("找到客户端"); InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); OutputStream os= socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); char chs[] = new char[3]; int index =0; float r=0; while(isr.read(chs)!=-1){ int a = Integer.parseInt(String.valueOf(chs[0])); int b = Integer.parseInt(String.valueOf(chs[2])); if(chs[1]=='+') { r =a+b; pw.write(String.valueOf(r)); pw.flush(); System.out.println(r); } else if(chs[1]=='-') { r =a-b; pw.write(String.valueOf(r)); pw.flush(); System.out.println(r); } else if(chs[1]=='*') { r =a*b; pw.write(String.valueOf(r)); pw.flush(); System.out.println(r); } else if(chs[1]=='/') { r =a/b; pw.write(String.valueOf(r)); pw.flush(); System.out.println(chs); System.out.println(r); } else { pw.write("Error"); pw.flush(); System.out.println(chs); System.out.println("Error"); } } /*Socket ssocket =new Socket("localhost",8889); OutputStream os =ssocket.getOutputStream(); OutputStreamWriter osw= new OutputStreamWriter(os);*/ isr.close(); is.close(); socket.close(); ss.close(); }catch(Exception e){e.printStackTrace();} } }
第三题:数据库操作,算平均分
import java.sql.*; public class Task3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String url ="jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password =""; String driver ="com.mysql.jdbc.Driver"; Connection conn =null; PreparedStatement sql =null; ResultSet rs = null; int id=0; String name=null; float java = 0; float j2ee =0; float aver = 0; try{ Class.forName(driver); conn =DriverManager.getConnection(url,user,password); System.out.println("连接数据库成功"); sql = conn.prepareStatement("select * from score where 1"); rs = sql.executeQuery(); while(rs.next()) { id = rs.getInt(1); name = rs.getString(2); java = rs.getFloat(3); j2ee = rs.getFloat(4); aver = rs.getFloat(5); System.out.println("ID:"+id+" Name:"+name+" Java:"+java+" J2EE"+j2ee+" AverageScroe:"+aver); } System.out.println("计算平均成绩并存入数据库"); sql = conn.prepareStatement("select * from score where 1"); rs = sql.executeQuery(); while(rs.next()) { id = rs.getInt(1); name = rs.getString(2); java = rs.getFloat(3); j2ee = rs.getFloat(4); aver =(java+j2ee)/2; sql = conn.prepareStatement("update score set averagescroe='"+aver+"' where id='"+id+"'"); sql.executeUpdate(); } System.out.println("输出数据"); sql = conn.prepareStatement("select * from score where 1"); rs = sql.executeQuery(); while(rs.next()) { id = rs.getInt(1); name = rs.getString(2); java = rs.getFloat(3); j2ee = rs.getFloat(4); aver = rs.getFloat(5); System.out.println("ID:"+id+" Name:"+name+" Java:"+java+" J2EE"+j2ee+" AverageScroe:"+aver); } rs.close(); sql.close(); conn.close(); }catch(Exception e){e.printStackTrace();} } }