zoukankan      html  css  js  c++  java
  • 第十四周总结

    package demo;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    public class Books {
        JFrame f;
        MenuBar mb;    
        Menu mu;      
        JTextArea jta;
        MenuItem openItem, saveItem, closeItem;   
        FileDialog openDia,saveDia;   
        
        File file;
        
        public Books()
        {
            init();
        }
        public void init()
        {
            f=new JFrame("简易记事本");
            mb=new MenuBar();
            mu=new Menu("文件");
            openItem=new MenuItem("打开");
            saveItem=new MenuItem("保存");
            closeItem=new MenuItem("退出");
            jta=new JTextArea();
            
            f.add(jta);
            
            mu.add(openItem);
            mu.add(saveItem);
            mu.add(closeItem);
            
            mb.add(mu);
            
            f.setMenuBar(mb);
            
            openDia=new FileDialog(f, "打开", FileDialog.LOAD);
            saveDia=new FileDialog(f, "保存", FileDialog.SAVE);
            
            f.setBounds(200, 300, 500, 400);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            
    
            event();
            
        }
        
        public void event()
        {
            openItem.addActionListener(new ActionListener()
            {
    
                public void actionPerformed(ActionEvent e) {
                    
                    openFile();
                                
                }
                
                
            });
            
    
            saveItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e) {
                 saveFile();    
                    
                }
                
                
            });
        
            jta.addKeyListener(new KeyAdapter()
            {
                public void keyPressed(KeyEvent e){
        
                    if(e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S)
                    {
                         saveFile();    
                    }
                }
            });
            
        
            closeItem.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                    
                }
                
            });
        }
        
    
        public void openFile()
        {
            openDia.setVisible(true); 
            String dirPath=openDia.getDirectory();
            String fileName=openDia.getFile();
            
        
            if(dirPath==null || fileName==null)
                return ;
            
            jta.setText(""); 
            file=new File(dirPath,fileName); 
            
            try
            {
                BufferedReader br = new BufferedReader(new FileReader(file));
     
                String line = null;
     
                while((line=br.readLine())!=null)
                {
                    jta.append(line+"
    ");
                }
     
                br.close();
            }
            catch (IOException ex)
            {
                throw new RuntimeException("读取失败");
            }
        }
        public void saveFile()
        {
        
            if(file==null)
            {
                saveDia.setVisible(true);
                
                String dirPath = saveDia.getDirectory();
                String fileName = saveDia.getFile();
                
                if(dirPath==null || fileName==null)
                    return ;    
                file = new File(dirPath,fileName);                
            }
            
    
            try {
                BufferedWriter bw=new BufferedWriter(new FileWriter(file));
                
                String info=jta.getText();  
                
                bw.write(info); 
                bw.flush();
                bw.close();
                
            } catch (IOException e1) {
                
                throw new RuntimeException();
            }        
            
        }
        
        
        public static void main(String[] args) {
            
              
            new Books();
        }
     
    }

    简单记事本运行截图:

    本周小结:

    JDBC:全称:Java Database Connectivity,是用于在Java语言编程中与数据库连接的API。

    作用:

    1. 连接到数据库
    2. 创建SQL语句
    3. 在数据库中执行SQL语句
    4. 返回执行结果

    关键字

    1.DriverManager:用于管理数据库驱动列表,使用通信子协议将来自Java的连接请求与适当的数据库驱动程序相匹配

    用法:DriverManager(url,username,password)

    • url:地址,具有特殊写法,比如“jdbc:mysql//localhost:3306/table_name"
    • username:登录数据库的用户名
    • password:登录数据库的密码

    2.Connection:该类具有用于联系数据库的所有方法

    用法:Connect conn=DriverManager(url,username,password);

    3.Statement:将SQL语句提交到数据库

    用法:

    创建:Statement state=conn.createStatement();
    提交:提交分为两种方法:
    state.executeUpdate:用于增、删、改,修改成功返回收到影响的行数,修改失败则返回0
    state.executeQuery:用于查找,返回一个ResultSet类型

    4.ResultSet:在state对象成功执行用于查询的SQL语句的前提下,作为迭代器,遍历state对象

    作用:

    创建:ResultSet rs=state.executeQuery(select语句);
    遍历:rs.next()
    移动指针,若指向的数据不为null,则返回true;否则,返回false
    获取:根据数据类型分类获取,并输入要获取的字段在返回表中的位置(从1开始),如果成功返回收到影响的行数
    rs.getInt(ColumnIndex); //获取整数型
    rs.getString(ColumnIndex); //获取字符型
    举例说明:

    向数据库插入一条数据:

    import java.sql.*;
    import java.util.Scanner;
    
    public class JDBC1 {
        public static void main(String[] args) {
            Scanner scan=new Scanner(System.in);
            Connection conn=null;
            try{
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/cn_xien?useSSL=true&characterEncoding=utf8";
                String username="root";
                String password="0611";
                conn=DriverManager.getConnection(url,username,password);//连接数据库驱动
    
                String update="insert into user(uid,username,password) values(6,'孙狗','111111')";
                Statement state=conn.createStatement();//创建state对象
                int i=state.executeUpdate(update);
                System.out.println(i);//打印返回值,根据返回值判断是否成功
            }catch(Exception e){
                e.printStackTrace();
            }finally {
                try{
                    if(conn!=null){
                        conn.close();    //关闭连接
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    将从控制台获取的数据注入SQL语句

    import java.sql.*;
    import java.util.Scanner;
    
    public class JDBC2 {
        public static void main(String[] args) {
            Scanner scan=new Scanner(System.in);
            Connection conn=null;
            try{
                Class.forName("com.mysql.jdbc.Driver");
    
                String url="jdbc:mysql://localhost:3306/cn_xien?useSSL=true&characterEncoding=utf8";
                String username="root";
                String password="0611";
                conn= DriverManager.getConnection(url,username,password);
                //从控制台获取数据
                System.out.println("请输入ID:");
                int uid=scan.nextInt();
                System.out.println("请输入密码:");
                String upwd=scan.next();
    
                String select="update user set password=? where uid=?";
                PreparedStatement ps=conn.prepareStatement(select);
                ps.setString(1,upwd);    
                ps.setInt(2,uid); 
                int line = ps.executeUpdate();
                System.out.println(line);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    conn.close();    //关闭连接
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    代理模式之动态代理
    代理模式之静态代理
    基于Java类进行配置Spring
    Spring使用注解开发
    Spring的自动装配
    Bean的作用域
    Spring配置
    最全总结 | 聊聊 Python 办公自动化之 Excel(上)
    最全总结 | 聊聊 Python 数据处理全家桶(MongoDB 篇)
    最全总结 | 聊聊 Python 数据处理全家桶(Redis篇)
  • 原文地址:https://www.cnblogs.com/hhwcg/p/11960622.html
Copyright © 2011-2022 走看看