当前位置:网站首页>jdbc入门
jdbc入门
2022-07-29 07:05:00 【蓝鲸不蓝369】
文章目录
jdbc入门

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详解
DriverManager

connection详解


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("注册成功");
}else
{
System.out.println("注册失败");
}
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("注册成功");
}else
{
System.out.println("注册失败");
}
preparedStatement.close();
connection.close();
}
}
preparedStatement原理

数据库连接池

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);
}
}
案例
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
);
边栏推荐
- QT专题:基础部件(按钮类,布局类,输出类,输入类,容器类)
- JS day 4 process control (if statement and switch statement)
- Excel file reading and writing (creation and parsing)
- My personal website doesn't allow access to wechat, so I did this
- I'd like to ask, my flick job writes data in the way of upsert Kafka, but I'm more careful in MySQL
- mysql 单表最多能存多少数据?
- ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
- Gin template
- route的meta配置项
- Homebrew brew update doesn't respond for a long time (or stuck in updating homebrew...)
猜你喜欢

使用自定义注解校验list的大小

PAT甲级 1146 拓扑顺序

H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines

以太网接口介绍

Ethernet interface introduction

QT连接两个qslite数据库报错QSqlQuery::exec: database not open

2-unified return class dto object

MySQL advanced (Advanced) SQL statement (I)

QT topic: basic components (button class, layout class, output class, input class, container class)

亚马逊云助手小程序来啦!
随机推荐
JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?
halcon的安装以及在vs2017中测试,vs2017中dll的配置
Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
Vite3.0 has been released, can you still roll it (list of new features)
[redis] redis development specifications and precautions
CDC source can quit after reading MySQL snapshot split
Clock tree synthesis (I)
在线问题反馈模块实战(十七):实现excel模板在线下载功能
MySQL----多表查询
Latest 10 billion quantitative private placement list
论文阅读 (62):Pointer Networks
Nodejs installation tutorial
Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
0 9 布隆过滤器(Bloom Filter)
Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
Why does ETL often become ELT or even let?
WPF simple login page completion case
logback简介及引入方法
我想问一下,我flink作业是以upsert-kafka的方式写入数据的,但是我在mysql里面去更
MySQL - multi table query