jdbc java_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中方式:

static Connection getConnection(String url)

static Connection getConnection(String url, Properties info)

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值