zoukankan      html  css  js  c++  java
  • 连接mysql数据库2+操作入门

    package 对数据库操作Statment;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Scanner;
    
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    public class query {
        /**
         * query
         * 
         * @throws Exception
         */
        @Test
        public void query() throws Exception {
            // 声明ststment对象用语执行sql
            Statement st = con.createStatement();
            // sql
            String sql = "select * from stud";
            // 查询返回结果集
            ResultSet rs = st.executeQuery(sql);
            // 遍历
            while (rs.next()) {
    
                String id = rs.getString("id");
                String name = rs.getString("name");
                System.err.println("编号:" + id + "," + "姓名" + name);
    
            }
    
        }
    
        /**
         * 删除
         * 
         * @throws Exception
         */
        @Test
        public void del() throws Exception {
            Scanner sc = new Scanner(System.in);
            System.err.println("plz enter id for del:");
            String id = sc.nextLine();
    
            Statement st = con.createStatement();
            String sql = "delete from stud where id=" + id;
            System.err.println("sql is:" + sql);
            st.executeUpdate(sql);
        }
    
        /**
         * save
         * 
         * @throws Exception
         */
        @Test
        public void save() throws Exception {
            Scanner sc = new Scanner(System.in);
            System.err.println("输入id");
            String id = sc.nextLine();
            System.err.println("输入name");
            String nm = sc.nextLine();
            Statement st = con.createStatement();
            String sql = "insert into stud values(" + id + ",'" + nm + "')";
            System.err.println("sql is:" + sql);
            st.executeUpdate(sql);
    
        }
    
        @Before
        // 执行Test前执行
        public void getCon() throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/abc?useUnicode=true&characterEncoding=utf8";
            con = DriverManager.getConnection(url, "root", "1234");
            // con.close();
            // System.err.println(con);
    
        }
    
        @After
        // 执行Test后执行
        public void closeConn() throws Exception {
            if (con != null || !con.isClosed()) {
    
                con.close();
            }
    
        }
    
        private Connection con;
    
    }
  • 相关阅读:
    VS2010 VC Project的default Include设置
    Linux 下的编辑/编译器
    用命令实现Win7远程桌面关机和重启
    怎样快速刪除Word中超链接?
    chrome浏览器世界之窗浏览器的收藏夹在哪?
    代码量查找工具[最好用的]
    C项目实践--网络协议和套接字编程
    memmove 和 memcopy
    bzoj2456: mode
    bzoj1205: [HNOI2005]星际贸易
  • 原文地址:https://www.cnblogs.com/xiaweifeng/p/3683879.html
Copyright © 2011-2022 走看看