Java中使用JDBC

JDBC简介

JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。

本文中中使用的数据库

数据库软件:MySQL5.6

数据库:test

表:student,其表结构如下:

+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| id           | int(11)     | NO   | PRI | 0       |       |
| name         | varchar(20) | YES  |     | NULL    |       |
| english      | float       | YES  |     | NULL    |       |
| math         | float       | YES  |     | NULL    |       |
| birthday     | date        | YES  |     | NULL    |       |
| native_place | varchar(30) | YES  |     | NULL    |       |
| chinese      | int(11)     | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+

| student | CREATE TABLE `student` (
  `id` int(11) NOT NULL DEFAULT '0',
  `name` varchar(20) DEFAULT NULL,
  `english` float DEFAULT NULL,
  `math` float DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  `native_place` varchar(30) DEFAULT NU
  `chinese` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

JDBC的使用

  • 建立连接

建立链接总共3中方式:

    1. static Connection getConnection(String url)
    2. static Connection getConnection(String url, Properties info)
    3. static Connection getConnection(String url, String user, String password)

本文中将使用第3中方式。

Connection conn=null; 
try { 
           conn=DriverManager.getConnection(url_conn, user_conn, passwd_conn); 
} catch (SQLException e) { 
           e.printStackTrace(); 
}

 

  • 获取statement
Statement st=null; 
        try { 
            st=conn.createStatement(); 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        }

 

  • 执行SQL语句
    • 1-查询
    • 2-插入
    • 3-删除
    • 4-修改

下面为查询的示例:

String sql_query_all_studnet="select * from student"; 
        ResultSet res=null; 
        try { 
            res=st.executeQuery(sql_query_all_studnet); 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        } 
        try { 
            System.out.println("id"+"\t"+"name"+"\t"+"chinese"+"\t"+"math"+"\t"+"english"); 
            while(res.next()) 
            { 
                int id=res.getInt("id"); 
                String name=res.getString("name"); 
                int chinese=res.getInt("chinese"); 
                double math=res.getDouble("math"); 
                double english=res.getDouble("english"); 
                System.out.println(id+"\t"+name+"\t"+chinese+"\t"+math+"\t"+english); 
            } 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        }

 

结果:

id  name chinese  math    english
1    潘怡茹    97    91.0    86.0
2    刘濮松    96    68.0    88.0
3    刘吉如    70    53.0    85.0
4    李岩珂    96    70.0    85.0
5    王晓博    46    79.0    85.0
6    李帅旭    97    76.0    79.0
7    李静瑶    92    61.0    89.0
8    金纾凡    83    43.0    80.0
9    秦梓航    86    46.0    57.0
10    关颖利    84    77.0    80.0

  • 关闭连接
try { 
            st.close(); 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        } 
        try { 
            conn.close(); 
        } catch (SQLException e) { 
            e.printStackTrace(); 
        }

 

posted @ 2015-05-02 19:07  ithaibo  阅读(291)  评论(0)    收藏  举报
请留下你的高见