package com.jdbc; import java.sql.*; public class JDBCDemo { public static void main(String[] args) throws ClassNotFoundException, SQLException { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.连接 String url = "jdbc:mysql://localhost:3306/zfj"; String username = "root"; String password = "root"; Connection con = DriverManager.getConnection(url,username,password); //3.语句执行对象 (执行sql) 返回值 Statement Statement stat = con.createStatement(); //4.执行sql语句 executeLargeUpdate 用与mysql 中 insert update delete //int row = (int) stat.executeLargeUpdate("INSERT INTO user (id,user_name,user_age,user_sex) VALUES ('100','fj','20','1')"); //查询 select String sql = "SELECT * FROM user"; ResultSet rs = stat.executeQuery(sql); //处理结果集 while (rs.next()){ //获取每列的的数据 System.out.println(rs.getString("id")+" "+rs.getString("user_name")+" "+rs.getString("user_age")+" "+rs.getString("user_sex")); } //5.释放资源 rs.close(); stat.close(); con.close(); } }