码迷,mamicode.com
首页 > 数据库 > 详细

JDBC

时间:2016-07-09 17:53:12      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

常用接口
DriverManage 驱动管理器 加载驱动
Connection 数据库连接接口
Statment  语句接口,静态操作SQL语句
ResultSet  结果集,保存数据记录的结果集合
ResultSetMetaData  结果集元数据,如:列名称,列类型等
DatabaseMetaData  数据元数据,如:数据库名称,版本等
JDBC的编程步骤:
-加载数据库驱动(jar文件)
-获得数据库连接
-创建语句
-执行查询
-遍历结果集
-关闭数据库连接
  1. package com.hei.jdbcdemo;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class Test {
  8. public static void main(String[] args) {
  9. Connection conn = null;
  10. try {
  11. Class.forName("com.mysql.jdbc.Driver");
  12. } catch (ClassNotFoundException e) {
  13. e.printStackTrace();
  14. }
  15. try {
  16. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. }
  20. String sql="select id,name from student";
  21. try {
  22. Statement stmt = conn.createStatement();
  23. ResultSet rs=stmt.executeQuery(sql);
  24. while(rs.next()){
  25. int id = rs.getInt(1);
  26. String name = rs.getString(2);
  27. System.out.println("id:"+id+",name:"+name);
  28. }
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }finally{
  32. if(conn!=null){
  33. try {
  34. conn.close();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. }
加入配置文件
  1. Properties prop = new Properties();
  2. Reader in = new FileReader("src\\config.propertions");
  3. prop.load(in);
  4. driver=prop.getProperty("driver");
  5. url=prop.getProperty("url");
  6. username=prop.getProperty("username");
  7. password=prop.getProperty("password");
Statement语句
1. statement 语句是SQL语句的描述,使用它可以操作各种SQL语句,包括DDL,DML,DCL等
2.使用Statement创建表:
  1. public class Test2 {
  2. public static void main(String[] args) {
  3. //createTable();
  4. //insert();
  5. //updata();
  6. //delete();
  7. query();
  8. }
  9. static void createTable(){
  10. Connection conn = DBUtil.open();
  11. String sql = "create table UserTbl(id int primary key auto_increment,name varchar(20))";
  12. try {
  13. Statement stmt = conn.createStatement();
  14. stmt.execute(sql);
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }finally{
  18. DBUtil.close(conn);
  19. }
  20. }
  21. static void insert(){
  22. Connection conn = DBUtil.open();
  23. String sql = "insert UserTbl Values(NULL,‘Tom‘)";
  24. try {
  25. Statement stat = conn.createStatement();
  26. stat.executeUpdate(sql);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }finally{
  30. DBUtil.close(conn);
  31. }
  32. }
  33. static void updata(){
  34. Connection conn = DBUtil.open();
  35. String sql = "update UserTbl set name=‘BigTom‘ where id>3";
  36. try {
  37. Statement stat = conn.createStatement();
  38. stat.executeUpdate(sql);
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }finally{
  42. DBUtil.close(conn);
  43. }
  44. }
  45. static void delete(){
  46. Connection conn = DBUtil.open();
  47. String sql = "delete from UserTbl where id=1";
  48. try {
  49. Statement stat = conn.createStatement();
  50. stat.executeUpdate(sql);
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }finally{
  54. DBUtil.close(conn);
  55. }
  56. }
  57. static List<User> query(){
  58. Connection conn = DBUtil.open();
  59. String sql = "select id,name from UserTbl";
  60. List<User> list= new ArrayList<User>();
  61. try{
  62. Statement stmt = conn.createStatement();
  63. ResultSet rs = stmt.executeQuery(sql);
  64. while(rs.next()){
  65. int id = rs.getInt(1);
  66. String name = rs.getString(2);
  67. User u = new User();
  68. u.setId(id);
  69. u.setName(name);
  70. list.add(u);
  71. //System.out.println("id"+id+" name"+name);
  72. }
  73. System.out.println(list);
  74. }catch(Exception e){
  75. e.printStackTrace();
  76. }finally{
  77. DBUtil.close(conn);
  78. }
  79. return list;
  80. }
  81. }
PreparedStatment
预定义语句继承Statment
  1. static void insert(String name,int id){
  2. String sql = "insert into UserTbl(id,name)values(?,?)";
  3. Connection conn = DBUtil.open();
  4. try {
  5. PreparedStatement pstmt = conn.prepareStatement(sql);
  6. pstmt.setInt(1,id);
  7. pstmt.setString(2, name);
  8. pstmt.executeUpdate();
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }finally{
  12. DBUtil.close(conn);
  13. }
  14. }


ResultSet结果集
打印出乱码的3个可能因素:
数据库编码不为 utf-8,jbk
数据表编码不为 utf-8,jbk
url编码不为 utf-8,jbk  
?useUnicode=true&characterEncoding=utf8
  1. public static List<Student> select(){
  2. Connection conn=DBUtil.open();
  3. List<Student> list = new ArrayList<Student>();
  4. String sql ="select * from Stud";
  5. try {
  6. Statement stat =conn.createStatement();
  7. ResultSet rs = stat.executeQuery(sql);//结果集
  8. while(rs.next()){
  9. int id = rs.getInt(1);
  10. String name = rs.getString(2);
  11. int age = rs.getInt(3);
  12. String sex = rs.getString(4);
  13. Date date = rs.getDate(5);
  14. Student student=new Student();
  15. student.setAge(age);
  16. student.setDate(date);
  17. student.setId(id);
  18. student.setName(name);
  19. student.setSex(sex);
  20. list.add(student);
  21. }
  22. for (Student student : list) {
  23. System.out.println(student);
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }finally{
  28. try {
  29. conn.close();
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. } return list;
  34. }





JDBC

标签:

原文地址:http://www.cnblogs.com/songwenyi/p/5656111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!