黑马程序员技术交流社区

标题: 笔记 [打印本页]

作者: feel10    时间: 2019-5-31 13:02
标题: 笔记
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcDemo01 {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "root");
        //定义sql语句
//        String sql = "update student set money=5000 where name ='关之琳'";
        String sql ="select*from student";
        //5.获取执行sql的对象 Statement
        Statement state = conn.createStatement();
        //执行sql
//        int count = state.executeUpdate(sql);
        ResultSet result = state.executeQuery(sql);
       while (result.next()){
           String name = result.getString("name");
           int money = result.getInt("money");
           System.out.println(name+".."+money);

       }

        //处理结果
      result.close();
        conn.close();
        state.close();

    }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2