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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

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