当前位置:网站首页>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
);
边栏推荐
- Scala higher order (10): exception handling in Scala
- Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
- logback appender简介说明
- Practice of online problem feedback module (XVII): realize the online download function of excel template
- Vagrant box cluster processing
- halcon的安装以及在vs2017中测试,vs2017中dll的配置
- 如何使用gs_expansion扩展节点
- js中break与continue和return关键字
- WPF simple login page completion case
- Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
猜你喜欢

在线问题反馈模块实战(十七):实现excel模板在线下载功能

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

MySQL advanced (Advanced) SQL statement (I)

0 9 布隆过滤器(Bloom Filter)

halcon的安装以及在vs2017中测试,vs2017中dll的配置

js中break与continue和return关键字

QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples

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

第7节-程序的编译(预处理操作)+链接

Operator3 - design an operator
随机推荐
When NPM is installed, it is stuck. There are five solutions
力扣(LeetCode)209. 长度最小的子数组(2022.07.28)
Docker最新超详细教程——Docker创建运行MySQL并挂载
MySQL 高级(进阶) SQL 语句 (一)
WPF nested layout case
Use vscode to configure Mysql to realize connection, query, and other functions
Vue router route cache
Excel文件读写(创建与解析)
0 9 布隆过滤器(Bloom Filter)
Job 7.28 file IO and standard IO
Vagrant box cluster processing
反射reflect
Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem
【Unity实战100例】Unity万能答题系统之单选多选判断题全部通用
Redis Basics
MySQL advanced (Advanced) SQL statement (I)
Fillder use
Life cycle hooks in routing - activated and deactivated
Variables and encryption in ansible
第7节-程序的编译(预处理操作)+链接