当前位置:网站首页>JDBC connection database
JDBC connection database
2022-04-23 06:00:00 【hanyc..】
Main steps :
1、 Load database driver
2、 Get database connection ( It includes getting user information 、url)
3、 Create execution sql The object of
4、 perform sql sentence
5、 Returns the result set of the execution
6、 Release the connection
package com.hyc.study01;
import java.sql.*;
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1、 Load database driver
Class.forName("com.mysql.jdbc.Driver");
//2、 Get user information 、url
//jdbcstudy Is the name of the database to connect to
//useUnicode=true&characterEncoding=utf8&useSSL=true Support Chinese coding & Set the Chinese character set to utf8& Using secure connections
String url =
"jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String psw = "123456";
//3、 Get database connection
// there Connection It represents the database , If the returned object is not empty , On behalf of the database, you get
Connection connection = DriverManager.getConnection(url, username, psw);
//4、 Created to perform sql The object of
Statement statement = connection.createStatement();
//5、 perform sql sentence
//6、 Return execution result set
String sql = "SELECT * FROM users";
// Execute the query sql sentence , Get the returned result set
ResultSet resultSet = statement.executeQuery(sql);// The returned result set is in the form of a linked list
while (resultSet.next()) {
System.out.println("id=" + resultSet.getInt("id"));
System.out.println("name=" + resultSet.getString("NAME"));
System.out.println("psw=" + resultSet.getString("PASSWORD"));
System.out.println("email=" + resultSet.getString("email"));
System.out.println("birth=" + resultSet.getDate("birthday"));
System.out.println("=====================================================");
}
//int num = statement.executeUpdate("add delete update sql");
// All additions, deletions and modifications are used statement.executeUpdate(), This method returns the execution sql The number of lines affected by the statement
//7、 Release the connection
resultSet.close();
statement.close();
connection.close();
}
}
Database and database tables :


版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541160359.html
边栏推荐
- 建表到页面完整实例演示—联表查询
- umi官网yarn create @umijs/umi-app 报错:文件名、目录名或卷标语法不正确
- 在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题
- PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
- 线性代数第二章-矩阵及其运算
- DBCP使用
- Pytorch learning record (7): skills in processing data and training models
- RedHat6之smb服务访问速度慢解决办法记录
- Postfix变成垃圾邮件中转站后的补救
- sklearn之 Gaussian Processes
猜你喜欢

Practical operation - Nacos installation and configuration

Pytorch learning record (XII): learning rate attenuation + regularization

PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包

Pyqy5 learning (III): qlineedit + qtextedit

深度学习基础——简单了解meta learning(来自李宏毅课程笔记)

Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering

Implementation of displaying database pictures to browser tables based on thymeleaf

图解numpy数组矩阵

delete和truncate

Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
随机推荐
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
Pyemd installation and simple use
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
Get the value of state in effects in DVA
Gaussian processes of sklearn
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
PyQy5学习(二):QMainWindow+QWidget+QLabel
Pytorch learning record (XII): learning rate attenuation + regularization
PyQy5学习(三):QLineEdit+QTextEdit
Linear algebra Chapter 2 - matrices and their operations
JVM family (4) -- memory overflow (OOM)
PreparedStatement防止SQL注入
常用编程记录——parser = argparse.ArgumentParser()
Fundamentals of digital image processing (Gonzalez) I
Implementation of displaying database pictures to browser tables based on thymeleaf
Development environment EAS login license modification
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
protected( 被 protected 修饰的成员对于本包和其子类可见)