1.注册驱动 Class.forname("com.mysql.jdbc.Driver");//这是连接mysql数据库的驱动
2.获取数据库连接 java.sql.Connection conn=java.sql.DriverManager.getConnection();
3.获取表达式 java.sql.Statement stmt=conn.createStatement("jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK","root","null");//三个参数分别是数据库连接的URL,用户名,密码
4.执行SQL java.sql.ResultSet rs=stmt.executeQuery("select * from user");
5.显示结果集里面的数据 while(rs.next()){ System.out.println(rs.getInt(1)); System.out.println(rs.getString("username")); System.out.println(rs.getString("password")); System.out.pringln(); }
//执行插入语句 //stmt.executeUpdate("insert into user values(1,'中文','345')");
6.释放资源 rs.close(); stmt.close(); conn.close();
|