Java连接数据库 七个步骤 JDBC

//1.在项目上导入对应的数据库jar包
//2.利用反射,加载数据库驱动
    Class.forName("com.mysql.jdbc.Driver");
//3.建立连接
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","root");
//4. 建立statement sql语句执行器   
    Statement stat=conn.createStatement();
//5.执行sql语句得到结果
    ResultSet rs=stat.executeQuery("select * from empolyee");
    int  rs =stat.executeUpdate(sql)
//查询用   executeQuery    增删改用executeUpdate返回int 修改的条数
//6.对得到的结果进行操作
    while(rs.next()) {
        System.out.print(rs.getInt("id")+"");
        System.out.print(rs.getString("name")+"");
        System.out.print(rs.getString("sex")+"");
        System.out.print(rs.getInt("age"));
        System.out.println();
    }       
//7.关闭
    rs.close();
    stat.close();
    conn.close();

猜你喜欢

转载自blog.csdn.net/Author1thy/article/details/81434078