惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 【当耐特】
The Cloudflare Blog
B
Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 叶小钗
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
A
About on SuperTechFans
雷峰网
雷峰网
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler

Sunshine

SpringBoot定时任务@Scheduled注解详解 | Sunshine 大端模式、小端模式解析 | Sunshine 机器数、原码、反码、补码解析 | Sunshine Spring事件驱动模型实践 | Sunshine IDEA好用的几款插件 | Sunshine Linux使用yum安装MySQL详细步骤(CentOS7.3) | Sunshine Windows下MySQL解压版安装配置详细步骤 | Sunshine Java使用JDBC连接SQLServer数据库(二) | Sunshine Git基本操作整理 | Sunshine Oracle序列创建和使用 | Sunshine Git批量清理本地分支 | Sunshine Statement.RETURN_GENERATED_KEYS获取自增id踩坑记录 | Sunshine GitPages+Hexo搭建个人博客 | Sunshine
Java使用JDBC连接SQLServer数据库(一) | Sunshine
DongyangHu · 2019-11-29 · via Sunshine

一、连接数据库

1、下载SQLServer对应的JDBC驱动

可从MySQL官网下载响应的驱动程序

2、将JDBC驱动导入项目中

将驱动jar包导入项目即可

3、测试是否导入成功

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.test;
public class Demo1 {
//驱动路径
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//程序入口函数
public static void main(String[] args) {
try {
//加载驱动程序
Class.forName(DBDRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
}

程序正常运行则证明数据库驱动程序配置成功。

4、创建数据库连接

例如:连接数据库名为:testdatabase的数据库,数据库登录用户名:sa,密码:123456的数据库。

代码如下:

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
package com.test;
import java.sql.*;
public class Demo1 {
//驱动路径
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//数据库地址
private static final String DBURL = "jdbc:sqlserver://localhost:1434;DataBaseName=testdatabase";
//数据库登录用户名
private static final String DBUSER = "sa";
//数据库用户密码
private static final String DBPASSWORD = "123456";
//数据库连接
public static Connection conn = null;
//程序入口函数
public static void main(String[] args) {
try {
//加载驱动程序
Class.forName(DBDRIVER);
//连接数据库
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

程序运行能打印出连接信息,则证明数据库连接正确。

二、数据库操作

1、查询数据

例:查询数据库testdatabasestudents表的数据。

代码如下:

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
package com.test;
import java.sql.*;
public class Demo1 {
//驱动路径
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//数据库地址
private static final String DBURL = "jdbc:sqlserver://localhost:1434;DataBaseName=testdatabase";
//数据库登录用户名
private static final String DBUSER = "sa";
//数据库用户密码
private static final String DBPASSWORD = "123456";
//数据库连接
public static Connection conn = null;
//数据库操作
public static Statement stmt = null;
//数据库查询结果集
public static ResultSet rs = null;
//程序入口函数
public static void main(String[] args) {
try {
//加载驱动程序
Class.forName(DBDRIVER);
//连接数据库
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
//实例化Statement对象
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from students");
while(rs.next()){
String Id = rs.getString(1);
String Name = rs.getString(2);
String Age = rs.getString(3);
String Sex = rs.getString(4);
System.out.print("学号:"+Id);
System.out.print(" 姓名:"+Name);
System.out.print(" 年龄:"+Age);
System.out.println(" 性别:"+Sex);
System.out.println("------------------------------------");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

2、更新数据

例:向数据库testdatabase中的students表添加一条记录。

代码如下:

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
package com.demo;

import java.sql.*;

public class Demo_sql {

//驱动路径
public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//数据库地址
public static final String DBURL = "jdbc:sqlserver://localhost:1434;DatabaseName=testdatabase";
//数据库登录用户名
public static final String DBUSER = "sa";
//数据库用户密码
public static final String DBPASSWORD = "123456";
//数据库连接
public static Connection conn = null;
//用于执行SQL语句
public static Statement stmt = null;
//SQL语句
public static final String SQLSTR = "insert into students values('201701170002','王思','21','女')";

public static void main(String[] args) {
try {
Class.forName(DBDRIVER);
System.out.println("成功连接!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
stmt = conn.createStatement();//实例化statement
stmt.executeUpdate(SQLSTR);
System.out.println("成功添加!");
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Sunshine