首页 > 数据库技术 > 详细

Java学习之JDBC(2)

时间:2016-02-24 15:35:01      阅读:284      评论:0      收藏:0      [点我收藏+]

上一篇已经简单介绍了如何使用JDBC连接数据库并且通过代码对数据库表进行操作。今天开始学习怎么通过代码向数据库中表插入记录。

因为我使用的是Intellij Idea 15.0.3,所以在查看数据库表的操作与Eclipse有所不同。一下是对数据库的配置

步骤:

(1)、View-->ToolWindows-->DataBase 打开DataBase的工作栏(一般在右侧),选择“+”号-->DataSource-->选择自己需要添加的数据库产品。

(2)、在其弹出的窗口中,填写主机名,端口号,数据库名,用户名及密码,填写完毕之后点击OK完成。

(3)、第二步完成之后在DataBase的工作区间中就可以看到你所添加的数据库。

 

配置完成之后,写一个Java程序测试一下。

以下是我的代码,和上一次的那道程序差不多。

 1 package JDBC;
 2 
 3 import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
 4 
 5 import java.sql.*;
 6 
 7 /**
 8  * Created by Lin-953 on 2016/2/24.
 9  */
10 public class ExexuteInsertStat {
11     public static void main(String[] args) {
12         Statement statement = null;
13         Connection connection = null;
14         try {
15             Class.forName("com.mysql.jdbc.Driver");
16 
17             String url = "jdbc:mysql://localhost:3306/structs";
18             String user = "root";
19             String passWord = "57295320.";
20 
21             connection = DriverManager.getConnection(url, user, passWord);
22             if (!connection.isClosed())
23                 System.out.println("Success Connect Mysql Client");
24 
25             String sql = "insert into student(Sno,Sname,Sex,BDate,Height) values(1430505,\"wer\",\"g\",\"2016-02-25\",156)";
26             String sql2 = "insert into student(Sno,Sname,Sex,BDate,Height) values(1430506,\"lxk\",\"b\",\"2016-02-02\",178)";
27             statement = connection.createStatement();
28 
29             //注意,executeUpdate方法返回的是一个int型的数值,插入成功返回成功插入的行数,否则返回0
30             int result = statement.executeUpdate(sql);
31             int Result = statement.executeUpdate(sql2);
32             int rows = result + Result;
33             
34             //插入成功或失败的提示
35             if (result != 0 && Result != 0)
36                 System.out.println("Insert Success" + "\tInsert " + rows + " rows");
37             else {
38                 if (result == 0 && Result == 0)
39                     System.out.println("Insert failed");
40                 else if (result != 0 && Result == 0)
41                     System.out.println("The second insert-statement insert failed");
42                 else
43                     System.out.println("The first insert-statement insert failed");
44             }
45         } catch (ClassNotFoundException e) {
46             e.printStackTrace();
47         } catch (SQLException e) {
48             e.printStackTrace();
49         } finally {
50             try {
51                 if (statement != null)
52                     statement.close();
53                 if (connection != null)
54                     connection.close();
55                 if (connection.isClosed())
56                     System.out.println("DisConnecct Mysql Client");
57             } catch (SQLException e) {
58                 e.printStackTrace();
59             }
60         }
61     }
62 }

 

最后我们运行一下看一下结果

技术分享技术分享

运行成功,两条insert语句执行成功,数据库中的表数据更新成功。。。

Java学习之JDBC(2)

原文:http://www.cnblogs.com/linxscake/p/5213127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!