您的位置:首页 > 数据库 > MySQL

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

2015-07-31 14:36 597 查看
比较常规的利用 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();

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jdbc 数据库 mysql