




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
于亚洲java笔记1. Utill包中类用法2. I/O输入输出3. Swing界面4. Jkbc连接数据库5. Xml解析6. 反射(reflect)7. 网络编程8. 线程(Thread)9. MysqlUtill包中类用法/计算用时long t1 = System.currentTimeMillis();long t2 = System.currentTimeMillis();System.out.println(t2-t1); /格式化日期和时间SimpleDateFormat类用法import java.text.SimpleDateFormat;SimpleDateFormat sdf = new SimpleDateFormat(yyyy-mm-dd hh:mm:ss hs);/hs为毫秒System.out.print(sdf.format(new Date();/Date用法Date date = new Date();date.getTime();/返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数为long类型date.toString();/把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat)。/ Calendar用法Calendar calendar = Calendar.getInstance();int year = calendar.get(Calendar.YEAR); /得到年int month = calendar.get(Calendar.MONTH) + 1; /得到月int date = calendar.get(calendar.DATE); /得到日int hour = calendar.get(calendar.HOUR);/得到时int minute = calendar.get(calendar.MINUTE);/得到分int second = calendar.get(calendar.SECOND);/得到秒int week = calendar.get(calendar.DAY_OF_WEEK);/得到星期的第几天,周日为1,周一为2String a = year+-+month+-+date+ +hour+:+minute+:+second+ 星期+week; System.out.println(a);Date date = calendar.getTime(); /Random用法Random r = new Random();int a = r.nextInt();double d = r.nextDouble();/list和set和map用法(容器类用法) List可依对象被放置至容器中的顺序来排列对象 Set不接受重复的对象/ArrayList类中的常用方法List list = new ArrayList();list.add(a);/后面加入值list.add(b);list.add(0,22);/在第0个位置插入”22”System.out.print(list.indexOf(b);/结果为2,为b的索引位置System.out.print(list.contains(a);/是否包含alist.remove(0);/移除第0个元素/三种遍历方式for(int i = 0;ilist.size();i+)System.out.print(list.get(i);/得到值for(String l : list) /必须映射 System.out.print(l); Iterator iterator = list.iterator();while(iterator.hasNext() System.out.print(iterator.next();list.clear();/清空全部list.isEmpty();/判断是否为空list.firstElement();/第一个元素list.lastElement();/最后一个元素ArrayList和Vector区别在Vector是线程安全的/以上代码用LindedList运行和ArrayList运行结果一样,用法一样/以下为LinkedList中增加的代码,如果频繁增删,用LinkedList效率更高addFirst();addLast();getFirst();getLast();removeFirst();removeLast();/HashSet中常用方法/比较两个加入HashSet容器中的对象是否相同时,会先比较hashCode()方法。如果相同,则再使用equals()方法比较,如果两者都相同,则视为相同的对象/如果想要在迭代时按照加入的顺序显示,则可以使用LinkedHashSet,用法相似,只是迭代输出时按顺序Set set = new HashSet();set.add(a);set.add(b);set.add(a);System.out.print(set.size();/结果为2,因为添加的不能重复/遍历for(String s : set) System.out.print(s);Iterator iterator = set.iterator();while(iterator.hasNext() System.out.print(iterator.next();set.contains(a);set.isEmpty();set.remove(a);/里面放的是object类型,因为不会重复set.clear();/TreeSet以字典顺序存储对象,可以按顺序输出/以上代码用HashSet运行和TreeSet运行结果一样,用法一样/以下为TreeSet中增加的代码set.first();set.last();/HashMap的用法HashMap map = new HashMap();map.put(name,张三);map.put(ID,1001);System.out.println(map.get(name);System.out.println(map.get(ID);/迭代器遍历Collection collection = map.values();Iterator iterator = collection.iterator();while(iterator.hasNext() System.out.print(iterator.next();for(String s : map.keySet()/得到键的set System.out.print(s);I/O输入输出/File的用法File file = new File(info.txt);System.out.println(文件是否存在:+file.exists();System.out.println(是否是文件:+file.isFile();System.out.println(是否是文件夹:+file.isDirectory();System.out.println(文件名:+file.getName();System.out.println(文件路径:+file.getPath();System.out.println(绝对路径:+file.getAbsolutePath();Date date = new Date(file.lastModified();System.out.println(文件最后修改时间:+date.toLocaleString();System.out.println(文件大小:+file.length();File file = new File(info1.txt);if(!file.exists()file.createNewFile(); file.delete();File f= new File(aaa);f.mkdir();/创建文件夹/得到文件夹中的文件File f = new File(path);File files = f.listFile();/得到文件夹中的所有文件for(File file : files)/RandomAccessFile将文本显示到屏幕上File file = new File(“stu.txt”);RandomAccessFile raf = new RandomAccessFile(file,rw);raf.seek(5);/跳到第五行String line = ;while(line=raf.readLine()!=null)System.out.println(line);/FileInputStream用法File file = new File(d:abc.text);FileInputStream fis = new FileInputStream(file);FileOutputStream fos = new FileOutputStream(d:abc1.text);int i = -1;while(i = fis.read() != -1) fos.write(i);fis.close();fos.close();/BufferedInputStream用法File file = new File(d:abc.text);FileInputStream fis = new FileInputStream(file);FileOutputStream fos = new FileOutputStream(d:abc2.text);BufferedInputStream bis = new BufferedInputStream(fis);BufferedOutputStream bos = new BufferedOutputStream(fos);byte i = new byte1024;while(bis.read(i) != -1) bos.write(i);bos.flush(); /刷新缓冲区bos.close();fos.close();bis.close();fis.close();/FileReader用法FileReader fr = new FileReader(file);FileWriter fw = new FileWriter(d:abc3.text);int i = -1;char ln = r, n;while(i=fr.read() != -1)if(i = n) /写入rn fw.write(ln);else fw.write(i);fw.flush();/BufferedReader用法File file = new File(d:abc.text);FileReader fr = new FileReader(file);FileWriter fw = new FileWriter(d:abc3.text);BufferedReader br = new BufferedReader(fr);BufferedWriter bw = new BufferedWriter(fw);String line = ;while(line = br.readLine() != null) bw.write(line); bw.newLine();bw.flush();bw.close();br.close();fw.close();fr.close();/DataOutputStream的用法FileOutputStream fos = new FileOutputStream(new File(data.txt);DataOutputStream dos = new DataOutputStream(fos);Employee emp1 = new Employee(zsan,20);Employee emp2 = new Employee(lisi,30);Employee emps = new Employee2;emps0 = emp1;emps1 = emp2;for(int i=0;iemps.length;i+)dos.writeUTF(empsi.getName();dos.writeInt(empsi.getAge(); dos.close();fos.close();FileInputStream fis = new FileInputStream(new File(data.txt);DataInputStream dis = new DataInputStream(fis);for(int i=0;iscreenSize.width)frameSize.width=screenSize.height;if(frameSize.widthscreenSize.width)frameSize.width=screenSize.width;/JFrame居中frame.setLocation(screenSize.width-frameSize.widh)/2,(screenSize.height-frameSize.height)/2)/界面添加标题setTitle(记事本);/设置图标Toolkit tk=Toolkit.getDefaultToolkit() Image image=tk.createImage(image.gif); /image.gif是你的图标this.setIconImage(image);/可见frame.setVisible(true);/设置大小和位置.setBounds(0,0,1024,860);/定位点,大小.setsize(1024,860);/关闭界面jFrame1.setVisible(false);jFrame1.dispose();/单选和复选框等获得选中状态jRadioButton1.setSelected(true);jCheckBox1.setSelected(true);/为下拉菜单添加内容JComboBox jComboBox1 = new JComboBox();jComboBox1.addItem(邯郸);/下拉框选中事件则在Event事件中选择“itemStateChanged”。/得到下拉框中的值jComboBox1.getSelectedItem().toString(); /得到选中字符Integer.parseInt(“123456”); /字符转数字/选择文件界面File file;JFileChooser jfc = new JFileChooser(c:);int value = jfc.showOpenDialog(this);if(value=0)file = jfc.getSelectedFile(); int n = jfc.showSavaDialog();if(n=0)File file = jfc.getSelectedFile();/上下选择SpinnerNumberModel snm = new SpinnerNumberModel(3, 1, 12,2);/定义模型 范围是1到12,初始化为3,一次加2JSpinner monthSpin = new JSpinner()monthSpin.setModel(snm);monthSpin.setPreferredSize(new Dimension(35, 20);/设置大小monthSpin.setName(Month); /名字/弹出警告对话框JOptionPane.showMessageDialog(null,我被点中了);/弹出选择对话框 int i = JOptionPane.showConfirmDialog(null, 真的要删除吗,删除操作, JOptionPane.YES_NO_OPTION);if(i=0)/填选是要执行的代码/为label加图和文字Icon icon = newImageIcon(c:/a.jpg);this.label1.setIcon(icon);label1.setText(于亚洲);/list的使用String data = one, two, three, four;JList myList = new JList(data);/显示右键菜单1.添加此方法private static void addPopup(Component component, final JPopupMenu popup) component.addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent e) if (e.isPopupTrigger() showMenu(e);public void mouseReleased(MouseEvent e) if (e.isPopupTrigger() showMenu(e);private void showMenu(MouseEvent e) popup.show(e.getComponent(), e.getX(), e.getY(););2.添加此组件JPopupMenu popupMenu = new JPopupMenu();addPopup(this, popupMenu);JMenu menu = new JMenu(查找);popupMenu.add(menu);3.添加单击事件textArea.addMouseListener(new MouseAdapter() Override public void mouseClicked(MouseEvent e) if(e.getButton()=e.BUTTON3) popupMenu.show(textArea, e.getX(), e.getY(); );/定义表格并加数据JTable jTable1 = new JTable();DefaultTableModel dtm = new DefaultTableModel (); /建模型dtm.addColumn(id); /添加标题dtm.addColumn(name);Vector vector = newVector(); /定义vectorvector.add(10016);vector.add(于亚洲);dtm.addRow(vector); /添加一行jTable1.setModel(dtm); /把模型加到表格中/清空表格中的全部数据for(int i=dtm.getRowCount()-1;i=0;i-)dtm.removeRow(i);/移除第i行/清空全部/得到选中的表格中的数据String col = jTable1.getModel().getValueAt(jTable1.getSelectedRow(),0).toString();int co=Integer.parseInt(col); /得选中行(jTable1.getSelectedRow()的第一个(0)数据/得到选中的行数和列数int I = jTable1.getSelectedColumn(); / getSelectedRow ()/设置表头最大宽度TableColumn tc=jTable1.getColumnModel().getColumn(0);tc.setMaxWidth(36);/最大宽度 tc.setPreferredWidth(2);/设宽度/表头不可移动jTable1.getTableHeader().setReorderingAllowed(false);/设置表格不可编辑见插件,让Dtm类代替DefaultTableModel;/设置表格调整方式 jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS); 列可调整的 5 个参数: AUTO_RESIZE_SUBSEQUENT_COLUMENS:当调整某一列宽时,此字段之后的所有字段列 宽都会跟着一起变动。此为系统默认值。 AUTO_RESIZE_ALL_COLUMNS:当调整某一列宽时,此表格上所有字段的列宽都会跟着一 起变动。 AUTO_RESIZE_OFF:当调整某一列宽时,此表格上所有字段列宽都不会跟着改变。 AUTO_RESIZE_NEXT_COLUMN:当调整某一列宽时,此字段的下一个字段的列宽会跟着改 变,其余均不会变。 AUTO_RESIZE_LAST_COLUMN:当调整某一列宽时,最后一个字段的列宽会跟着改变,其 余均不会改变。 /更换paneJPanel panel = (JPanel)getContentPane(); /定义新panelpanel.setSize(contentPanel.getSize(); /contentPanel为原panelpanel.setVisible(true);contentPanel.removeAll();contentPanel.add(panel);contentPanel.repaint();/建立TreeJScrollPane js = new JScrollPane();JTree jtree1 = new JTree();DefaultMutableTreeNode root = new DefaultMutableTreeNode(于亚洲);DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(zsan);root.add(childNode);jTree1 = new JTree(root);js.getViewport().add(jtree1);/选中事件DefaultMutableTreeNode node=(DefaultMutableTreeNode)this.jTree1.getLastSelectedPathComponent();node.isleaf();node.toString();Jdbc连接数据库/配置文件内容driver=com.mysql.jdbc.Driverurl=jdbc:mysql:/localhost:3306/mysql?useUnicode=true&characterEncoding=gb2312user=rootpsw=123456/读取配置文件Properties pro = new Properties();pro.load(new FileInputStream(perties);String url = pro.getProperty(url);String user = pro.getProterty(user);String psw = pro.getProperty(psw);/定义变量String driver=”com.mysql.jdbc.Driver”;String url=”jdbc:mysql:/localhost:3306/mysql?useUnicode=true&characterEncoding=gb2312”;String user=”root”;String psw=”123456”;/加载Class.forName(driver);/连接数据库Connection con=DriverManager.getConnection(url,user,psw);/分条执行String sql = insert into stu(id,name) values(10010,于亚洲);Statement sta = con.createStatement();int rows = sta.executeUpdate(sql); /返回受影响的行数if(rows0)System.out.println(执行成功);/ResultSet接收全部信息Statement sta = con.createStatement();ResultSet rs = sta.executeQuery(“select * from stu”);while(rs.next()int id = rs.getInt(id);/得到id值String name = rs.getString(name); /得到全部值ResultSetMetaData rsmd = rs.getMetaData();/ResultSetMetaData得到表头信息,表名、列名、字段型态System.out.println(表名t列名t列类型); for(int i=1;i=rsmd.getColumnCount();i+)System.out.println(rsmd.getTableName(i);System.out.println(rsmd.getColumnName(i);System.out.println(rsmd.getColumnTypeName(i);/批处理Statement sta = con.createStatement();sta.addBatch(insert into stu(id,name) values(10011,张三);sta.addBatch(insert into stu(id,name) values(10012,李四);int rows = sta.executeBatch();/预处理String sql = insert into stu(id,name) values(?,?);PreparedStatement pr=con.prepareStatement(sql);pr.setInt(1,10013);pr.setString(2,王五);int rows = pr.executeUpdate();/存二进制文件 在JDBC中也提供了java.sql.Blob与java.sql.Clob两个类别分别代表BLOB与CLOB资料File file = new File(./logo_phpbb.jpg);int length = (int) file.length();InputStream fin = new FileInputStream(file); /填入数据库PreparedStatement pstmt = conn.prepareStatement( INSERT INTO files VALUES(?, ?, ?);pstmt.setInt(1, 1);pstmt.setString(2, filename);pstmt.setBinaryStream (3, fin, length);pstmt.executeUpdate();pstmt.clearParameters();pstmt.close();fin.close();Blob blob = result.getBlob(2); /取得BLOBClob clob = result.getClob(2) /取得CLOBXml解析/SAX解析/需要导入jdom包FileInputStream fis = new FileInputStream(students.xml);SAXBuider saxb = new SAXBuider();/创建SAX解析器Document doc = sacb.build(his);/文档对象模型 需要导入org.jdom.DocumentElement root = doc.getRootElement();/获得根元素List lists = root.getRootElement();/获得根元素下子元素String name = root.getAttributeValue(name);/获取根节点的name属性/Dom解析导包时有org.w3c的导入它DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();/创建解析器工厂DocumentBuilder builder = factory.newDocumentBuilder();/创建文件解析器InputStream input =Main1.class.getResourceAsStream(students.xml);/ Main1为与XML文件在同一个包下的类Document doc = db.parse(input);或者用Document doc = builder.parse(student.xml);/获取文档对象/XML文件只能放在工程文件中NodeList nlist1 =doc.getChildNodes();/得到根节点Node node1 = nlist1.item(0);Element root = (Element)node1;/强转,Node是Element的父类NodeList nlist2 = root.getChildNodes();for(int i = 0;inlist2.getLength();i+)Node node =nlist2.item(i);if (node instanceof Element)Element element1 = (Element)node;Attr attr1 = element1.getAttributeNode(studentno);System.out.println(attr1.getNodeName()+attr1.getNodeValue();NodeList nodelist2 = element1.getChildNodes();for(int j=0;jnodelist2.getLength();j+)Node node2 = nodelist2.item(j);if(node2 instanceof Element)Element element2 = (Element)node2;Text text = (Text) element2.getFirstChild();System.out.println(text.getNodeValue();/用于获得局部值NodeList stus = doc.ge
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 村委大楼出租合同范本
- 租赁螺旋钻机合同范本
- 火锅商铺转让合同范本
- 设施外包合同范本
- 男鞋生产合同范本
- 年产10万套人工驱雷电设备生产线项目可行性研究报告模板-立项备案
- 山东建筑公司合同范本
- 铺面租赁拍卖合同范本
- 租人租车合同范本
- 花卉销售配送合同范本
- 2025年度泸州老窖白酒线上线下全渠道销售代理协议
- 教职工开学安全知识培训课件
- 2025至2030年中国焦炉气制LNG市场竞争格局及行业投资前景预测报告
- 2025年公路交通水运三类人员试题及答案
- 2025年河北省初中学业水平考试历史试题(含答案)
- 2025年甘肃省公职招录考试(省情时政)历年参考题库含答案详解(5套)
- 期末必考题检测卷(三)(含答案)高一数学下学期人教A版必修第二册
- 2025年江苏公务员遴选考试公文写作试卷(附答案)
- 2025年度以新质生产力助推高质量发展等继续教育公需科目试题及答案
- 2025年技师安全考试题库
- 站点考勤管理制度
评论
0/150
提交评论