JAVA操作Mysql数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/phpdb";
        String u = "root";
        String p = "sa123456";
         
        try {
            Class.forName(driver);
            Connection con = DriverManager.getConnection(url,u,p);
            if(!con.isClosed()){
                System.out.println("链接成功...");
                Statement statement = con.createStatement();
                String sql = "select * from articles";
                String insertSql = "insert into articles(title,content) values(?,?)";
                //执行查询操作
                ResultSet rs = statement.executeQuery(sql);
                 
                PreparedStatement ps = con.prepareStatement(insertSql);
                ps.setString(1, "哈哈哈1111");
                ps.setString(2, "写入数据成功啦....");
                //执行增、删、改操作
                ps.executeUpdate();
                 
                while(rs.next()){
                    System.out.println(rs.getString("content"));
                }
                rs.close();
                con.close();
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

JAVA操作Mysql数据库

上一篇:CentIOS PHP 扩展库


下一篇:SQL面试题