MySQL数据库之javaweb中mysql数据库连接方法
小标 2019-03-06 来源 : 阅读 1167 评论 0

摘要:本文主要向大家介绍了MySQL数据库之javaweb中mysql数据库连接方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之javaweb中mysql数据库连接方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

MySQL数据库之javaweb中mysql数据库连接方法

一、直接连接,不封装到工具类中,主要步骤:


先导包:mysql-connector-java-5.0.8-bin.jar(点击跳转到下载界面),放在WebRoot/WEB-INF/lib/下


1.加载驱动//com.MySQL.jdbc.Driver

2.获取连接 Connection对象

3.获取用于向数据库发送SQL的Statement对象

4.执行sql,获取数据,解析数据

5.关闭连接,释放资源


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

/*协议:子协议://主机:端口/数据库名*/

Stringurl="jdbc:mysql://localhost:3306/jdbctest";

 

//mysql数据库的用户名与密码,安装时自己设置,一般默认为root

Stringuser="root";

Stringpassword="root";

 

Connectionconnection=null;

Statementstatement=null;

ResultSetresultSet=null;

try{

//1.加载驱动//com.mysql.jdbc.Driver

/*

*DriverManager.registerDriver(new

*Driver());用这种方法会加载两次驱动,也就是说会创建两个drive对象

*/

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接

connection=DriverManager.getConnection(url,user,password);

 

//3.获取用于向数据库发送SQL的Statement对象

statement=connection.createStatement();

 

//4.执行sql,获取数据

resultSet=statement.executeQuery("SELECT*FROMusers;");

 

//解析数据

while(resultSet.next()){

intid=resultSet.getInt("id");

Stringname=resultSet.getString("name");

Stringpsd=resultSet.getString("password");

Stringemail=resultSet.getString("email");

Stringbirthday=resultSet.getString("birthday");

 

System.out.println(id+""+name+""+psd+""+email

+""+birthday);

}

}catch(ClassNotFoundExceptione){

e.printStackTrace();

}catch(SQLExceptione){

e.printStackTrace();

}finally{

 

//5.关闭连接,释放资源

if(resultSet!=null){

try{

resultSet.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

resultSet=null;

}

 

if(statement!=null){

try{

statement.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

statement=null;

}

 

if(connection!=null){

try{

connection.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

connection=null;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

/* 协议:子协议://主机:端口/数据库名 */

String url = "jdbc:mysql://localhost:3306/jdbctest";

 

// mysql数据库的用户名与密码,安装时自己设置,一般默认为root

String user = "root";

String password = "root";

 

Connection connection = null;

Statement statement = null;

ResultSet resultSet = null;

try {

    // 1.加载驱动//com.mysql.jdbc.Driver

    /*

     * DriverManager.registerDriver(new

     * Driver());用这种方法会加载两次驱动,也就是说会创建两个drive对象

     */

    Class.forName("com.mysql.jdbc.Driver");

    // 2.获取连接

    connection = DriverManager.getConnection(url, user, password);

 

    // 3.获取用于向数据库发送SQL的Statement对象

    statement = connection.createStatement();

 

    // 4.执行sql,获取数据

    resultSet = statement.executeQuery("SELECT * FROM users;");

 

    // 解析数据

    while (resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("name");

String psd = resultSet.getString("password");

String email = resultSet.getString("email");

String birthday = resultSet.getString("birthday");

 

System.out.println(id + " " + name + " " + psd + " " + email

" " + birthday);

    }

catch (ClassNotFoundException e) {

    e.printStackTrace();

catch (SQLException e) {

    e.printStackTrace();

finally 

 

        //5.关闭连接,释放资源

    if (resultSet != null) {

try {

    resultSet.close();

catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

resultSet = null;

    }

 

    if (statement != null) {

try {

    statement.close();

catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

statement = null;

    }

 

    if (connection != null) {

try {

    connection.close();

catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

connection = null;

    }

}


二、将数据库连接封装成一个工具类


这样做的好处是,在实际开发中,就能做到,改一处即可修改全局。


1.建一个名为db.properties的配置文件,放于src/


1

2

3

4

url=jdbc:mysql://localhost:3306/jdbctest

username=root

password=root

driver=com.mysql.jdbc.Driver


2.工具类:


本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程