JDBC 连接数据库实例(MySQL为例)

来源:互联网 发布:linux mysql开始连接 编辑:程序博客网 时间:2024/05/23 19:56

比较常规的利用 JDBC 进行数据库操作的几个例子,写下来以防需要。方法肯定不是最好的,此处只是给出几个基本的代码,具体可以根据需要进行优化或者整合。

并且 jdbc 绝对不止这么一点简单的东西。如果熟悉其中的类和方法,可以达到更好的操作体验。

1.加载驱动

            Class.forName("com.mysql.jdbc.Driver");

2.建立连接

            String url = "jdbc:mysql://localhost:3306/java?user = root&password = root";
            Connection conn = DriverManager.getConnection(url, "root", "root");

3.增删改查

    1)查:

public void select() throws SQLException {
            try {
                 Statement stmt = conn.createStatement();
                 String sql = "SELECT * FROM new_table";
                 ResultSet rs = stmt.executeQuery(sql);
                 while (rs.next()) {
                     System.out.print(rs.getInt(1)+" ");
                     System.out.print(rs.getString(2)+" ");
                     System.out.println(rs.getInt(3));
                 }
           } catch (SQLException e) {
                System.out.println("__Select Error__");
                e.printStackTrace();
            }finally {
                rs.close();
                stmt.close();
            }
    }


    2)删

public void delete(int id) throws SQLException {
        try{
            Statement stmt = conn.createStatement();
            String sql = "DELETE FROM new_table WHERE id =?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("__Delete Error__");
            e.printStackTrace();
        }finally {
            ps.close();
            stmt.close();
        }
    }


    3)增

    public void insert(User user) throws SQLException {
        try{
            int i = 0;
            Statement stmt = conn.createStatement();
            sql = "INSERT INTO new_table(id, name, age) value(?,?,?)";
            ps = (PreparedStatement)conn.prepareStatement(sql);
            ps.setInt(1, user.getId());
            ps.setString(2,user.getName());
            ps.setInt(3, user.getAge());
            i = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("__Insert Error__");
            e.printStackTrace();
        }finally {
            ps.close();
            stmt.close();
        }
    }


    4)改

public void update(User user) throws SQLException{
        try{
            int i= 0;
            Statement stmt = conn.createStatement();
            String sql = "update new_table set name ='"+user.getName()+"',age = '"+user.getAge()+"' where id = '"+user.getId()+"' ";
            PreparedStatement ps = (PreparedStatement)conn.prepareStatement(sql);
            i = ps.executeUpdate();
        }catch (SQLException e){
            System.out.println("__Update Error__");
            e.printStackTrace();
        }finally {
            ps.close();
            stmt.close();
        }
    }


    5)带东西的查

public void getById(int id) throws SQLException {
        try{
            Statement stmt = conn.createStatement();
            String sql = "select *from new_table where id = '"+id+"'";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                System.out.print(rs.getInt("id") + " ");
                System.out.print(rs.getString("name")+" ");
                System.out.println(rs.getInt("age"));
            }
        }catch (SQLException e){
            System.out.println("__Get by Id Error__");
            e.printStackTrace();
        }finally{
            ps.close();
            stmt.close();
        }
    }

0 0
原创粉丝点击