当前位置:网站首页>Getting started with JDBC
Getting started with JDBC
2022-07-29 07:29:00 【Blue whale not blue 369】
List of articles
jdbc introduction

package com.itheima.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JDBC {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String username="root";
String url="jdbc:mysql://localhost:3306/db1";
String password="1234q";
Connection connection = DriverManager.getConnection(url, username, password);
String sql="update account set money =2000 where id=1;";
Statement statement = connection.createStatement();
int count=statement.executeUpdate(sql);
System.out.println(count);
statement.close();
connection.close();
}
}
api Detailed explanation
DriverManager

connection Detailed explanation


statement

ResultSet


package com.itheima.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBC_ResultSet {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String username="root";
//String url="jdbc:mysql://localhost:3306/db1";
String url="jdbc:mysql:///db1?useSSL=false";
String password="1234";
Connection connection = DriverManager.getConnection(url, username, password);
/*String sql="update account set money =2000 where id=1;"; Statement statement = connection.createStatement(); int count=statement.executeUpdate(sql);*/
// System.out.println(count);
String sql="select *from account";
Statement statement=connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next())
{
int id=resultSet.getInt("id");
String name=resultSet.getString("name");
double money=resultSet.getDouble("money");
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("---------------------");
}
statement.close();
connection.close();
resultSet.close();
}
}
PreparedStatement

package com.itheima.jdbc;
import jdk.jfr.StackTrace;
import org.testng.annotations.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class userlogin {
@Test
public void testResultSet() throws Exception
{
// Class.forName("com.mysql.jdbc.Driver");
String username="root";
//String url="jdbc:mysql://localhost:3306/db1";
String url="jdbc:mysql:///db1?useSSL=false";
String password="1234";
Connection connection = DriverManager.getConnection(url, username, password);
String name="zhangsan";
String pwd="' or '1'='1";
String sql="select * from login where username='"+name+"' and userpassword='"+pwd+"'";
System.out.println(sql);
Statement statement = connection.createStatement();
ResultSet rs=statement.executeQuery(sql);
// System.out.println(count);
if(rs.next())
{
System.out.println(" Registered successfully ");
}else
{
System.out.println(" Registration failed ");
}
statement.close();
connection.close();
}
}

package com.itheima.jdbc;
import org.testng.annotations.Test;
import java.sql.*;
public class JDBC_Parparedstatment {
@Test
public void testResultSet() throws Exception
{
// Class.forName("com.mysql.jdbc.Driver");
String username="root";
//String url="jdbc:mysql://localhost:3306/db1";
String url="jdbc:mysql:///db1?useSSL=false";
String password="1234";
Connection connection = DriverManager.getConnection(url, username, password);
String name="zhangsan";
String pwd="123";
String sql="select * from login where username= ? and userpassword = ?";
System.out.println(sql);
//Statement statement = connection.createStatement();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,pwd);
ResultSet rs=preparedStatement.executeQuery();
// System.out.println(count);
if(rs.next())
{
System.out.println(" Registered successfully ");
}else
{
System.out.println(" Registration failed ");
}
preparedStatement.close();
connection.close();
}
}
preparedStatement principle

Database connection pool

package Druid;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Map;
import java.util.Properties;
public class Druiddemo {
public static void main(String[] args) throws Exception {
System.out.println(System.getProperty("user.dir"));
Properties prop=new Properties();
prop.load(new FileInputStream("D:/javacode/jdbcMaven/src/druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
Connection connection= dataSource.getConnection();
System.out.println(connection);
}
}
Case study
package Druid;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class BrandTest {
@Test
public void testSelectAll() throws Exception {
System.out.println(System.getProperty("user.dir"));
Properties prop=new Properties();
prop.load(new FileInputStream("D:/javacode/jdbcMaven/src/druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
Connection connection= dataSource.getConnection();
String sql="select * from tb_brand";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
List<Brand>list=new ArrayList<>();
while(resultSet.next())
{
Integer id=resultSet.getInt(1);
String brand_name=resultSet.getString(2);
String company_name=resultSet.getString(3);
Integer ordered=resultSet.getInt(4);
String description=resultSet.getString(5);
Integer status=resultSet.getInt(6);
Brand brand=new Brand(id,brand_name,company_name,ordered,description,status);
list.add(brand);
}
for (Brand brand : list) {
System.out.println(brand);
}
}
}
-- auto-generated definition
create table tb_brand
(
id int auto_increment
primary key,
brand_name varchar(20) charset utf8 null,
company_name varchar(20) charset utf8 null,
ordered int null,
description varchar(100) charset utf8 null,
status int null
);
边栏推荐
- How much data can a single MySQL table store at most?
- BeanUtils.setProperty()
- Amazon cloud assistant applet is coming!
- Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
- Segger's hardware anomaly analysis
- 对Vintage分析的一些学习理解
- Summer summary (II)
- 计算程序运行时间 demo
- 2-统一返回类DTO对象
- CMOS芯片制造全工艺流程
猜你喜欢

用户列表 圆形头像并跟随小板块

SEGGER 的硬件异常 分析

QT基础第二天(2)qt基础部件:按钮类,布局类,输出类,输入类,容器等个别举例

09 bloom filter

PAT甲级 1146 拓扑顺序

I, 28, a tester, was ruthlessly dismissed in October: I want to remind people who are still learning to test

Practice of online problem feedback module (XVII): realize the online download function of excel template

Error 1045 (28000) access denied for user 'root' @ 'localhost' solution

Docker's latest super detailed tutorial - docker creates, runs, and mounts MySQL

WPF interface layout must know basis
随机推荐
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
Docker's latest super detailed tutorial - docker creates, runs, and mounts MySQL
状态机dp三维
能在SQL 语句中 指定 内存参数吗?
halcon的安装以及在vs2017中测试,vs2017中dll的配置
论文阅读 (62):Pointer Networks
Zabbix 其他基础监控项
Some learning and understanding of vintage analysis
QT专题:基础部件(按钮类,布局类,输出类,输入类,容器类)
请问flink支持sqlServer数据库么?获取sqlServer数据库的变化
leetcode力扣经典问题——4.寻找两个正序数组的中位数
Can the subset of the array accumulate K
log4qt内存泄露问题,heob内存检测工具的使用
LevelFilter简介说明
Using C language to skillfully realize the chess game -- Sanzi chess
[daily question in summer] Luogu p6408 [coci2008-2009 3] pet
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
计算程序运行时间 demo
cdc source能读完MySqlSnapshotSplit 就退出嘛
Docker最新超详细教程——Docker创建运行MySQL并挂载