当前位置:网站首页>mysql/sql server通过JDBC连接数据库进行增删改查的步骤
mysql/sql server通过JDBC连接数据库进行增删改查的步骤
2022-07-21 05:02:00 【不爱赖床的懒虫】
mysql/sql server如何通过JDBC连接数据库进行增删改查
Java编程----通过idea或者eclipse连接数据库的步骤
在做数据库课程设计时遇到的一些问题与解决方案,希望能帮助大家少走点弯路。
1. mysql连接数据库
准备阶段
1.下载Java对应版本的JDBC驱动
mysql:https://www.mysql.com/products/connector/
sql server:https://www.microsoft.com/zh-CN/download/details.aspx?id=57175
2.加入库中
3.Java代码设置:
(注:红色字体处是需要根据自己的mysql配置所改动):
首先需要设置mysql的账户与密码
user:root(默认)
password:自拟
驱动:“com.mysql.cj.jdbc.Driver”
URL:“jdbc:mysql://localhost:3306/+数据库+?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC”
DriverManager.getConnection(dbURL, “user”, “password”);
代码实现阶段
本人的user:root,password:mysql ,数据库:yggl
import java.sql.*;
public class test_MySQL {
private static Connection dbConn = null;
public static void main(String[] args) {
try {
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("加载驱动成功!");
//2.连接
String dbURL = "jdbc:mysql://localhost:3306/yggl?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
dbConn = DriverManager.getConnection(dbURL, "root", "mysql");
System.out.println("连接数据库成功!");
//3.访问数据库
String sql="select * from Employees";//查询sql语句
Statement statement=dbConn.createStatement();
ResultSet res=statement.executeQuery(sql);
while(res.next()){
String title=res.getString("EmployeeID");
System.out.println(title);
}
res.close();
dbConn.close();
}catch(Exception e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}
}
}
2. sql server连接数据库
准备阶段
原理与mysql类似,但是需要更改IP为127.0.0.1,端口为1433(->SQL Server网络配置->TCP/IP属性中修改)
设置user:as
设置password:as
(确保为SQL Server身份验证,而不是Windows验证)
驱动:“com.microsoft.sqlserver.jdbc.SQLServerDriver”
URL:“jdbc:sqlserver://127.0.0.1:1433;DatabaseName=数据库”
DriverManager.getConnection(dbURL, “user”, “password”);
代码实现阶段
import java.sql.*;
public class test_SqlServer {
private static Connection dbConn = null;
public static void main(String[] args) {
try {
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("加载驱动成功!");
//2.连接数据库
String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ssgl";
dbConn = DriverManager.getConnection(dbURL, "sa", "sa");
System.out.println("连接数据库成功!");
//3.访问数据库
String sql="select * from stay_info where Dorm_num=105";
Statement statement=dbConn.createStatement();
ResultSet res=statement.executeQuery(sql);
while(res.next()){
String title=res.getString("Stu_num");
System.out.println(title);
}
res.close();
dbConn.close();
}catch(Exception e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}
}
}
3. 修改语句的不同处
查询sql语句和修改sql语句的相关代码有部分区别
不同处:
String sql1="create table stuinfo(studentid int not null, " +
"studentname varchar(10)," +
" profession varchar(10),age int, password varchar(10))";
String sql2="insert into stuinfo values (1001,'张三','本科',19,'1234')";
Statement statement=dbConn.createStatement();
statement.executeUpdate(sql1);
statement.executeUpdate(sql2);
如果还有不懂得欢迎留言!
边栏推荐
- 868. 筛质数
- STM32 - positioning module atgm336h, data analysis, longitude and latitude extraction
- 2022.7.19DAY610
- Real time tracking of user management operations
- 判断“String[]”数组中是否有存在重复的值,利用hashSet特性排查
- 对象拷贝工具类(fastjson)
- aws的postgreSQL是去掉了hba吗?
- 自动抓包工具 web scraper
- Project of integrating SSM framework
- MySql update语句或者delete语句 where条件没走索引会锁表
猜你喜欢
Flutter通过Geolocator定位插件获经纬度调用高德周边信息接口
STM32 - positioning module atgm336h, data analysis, longitude and latitude extraction
Gaode map API obtains the surrounding information corresponding to the current location
The JSON tool converts objects to JSON format strings
[C exercise] macro realizes the exchange of digital binary parity bits
Pycharm创建SQLite数据库
Share the experience of big factories, come quickly
Wechat official account development access, using wechat public platform to apply for test number for local development
fastJson数据类型中,解析JSONObject出现$ref: “$.list[0]“问题
One more blank page when printing the Jim building block report - problem solving
随机推荐
解决Oracle数据库查询单表排序顺序错误问题之一
How to successfully get offers from ant, jd.com, Xiaomi, Tencent and other major manufacturers
The JSON tool converts objects to JSON format strings
2022.7.18DAY609
Wechat official account development access, using wechat public platform to apply for test number for local development
Iwemeta: Shi Yuzhu's golden sauce wine: OEM factories have been shut down. Who is producing it?
Quartz create scheduled tasks (getting started)
从20s优化到500ms,我是如何做到的
Character function and memory function
FL Studio 20.9水果编曲软件中文汉化补丁包
Metauniverse iwemeta: the new cover of time magazine, metauniverse will change everything
Share pictures or link to Tiktok
629. K inverse pairs array
CentOS 8中Docker安装MySQL8
STL list merge
Summary of arithmetic operations in shell
869. 试除法求约数
@Scheduled scheduled task details
如何确定你访问的网站的真实性——证书体系
字符函数和内存函数