当前位置:网站首页>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
);
边栏推荐
猜你喜欢

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

Operator3-设计一个operator

Excel file reading and writing (creation and parsing)

Vite3.0都发布了,你还能卷得动吗(新特性一览)

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

JS break and continue and return keywords

Use vscode to configure Mysql to realize connection, query, and other functions

Excel文件读写(创建与解析)

Synchronous / asynchronous, blocking / non blocking and IO

2-统一返回类DTO对象
随机推荐
Using C language to skillfully realize the chess game -- Sanzi chess
0 8 动态规划(Dynamic Programming)
Scala 高阶(九):Scala中的模式匹配
H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines
Remote invocation of microservices
Paper reading (62):pointer networks
JS day 4 process control (if statement and switch statement)
QT连接两个qslite数据库报错QSqlQuery::exec: database not open
WPF simple login page completion case
Vite3.0都发布了,你还能卷得动吗(新特性一览)
How to establish EDI connection with Scania in Scania?
2-统一返回类DTO对象
能在SQL 语句中 指定 内存参数吗?
Leetcode 879. profit plan
Logback log level introduction
Personal blog system (with source code)
My personal website doesn't allow access to wechat, so I did this
[100 cases of unity practice] the single choice multiple choice judgment questions of unity universal question answering system are all common
请问flink支持sqlServer数据库么?获取sqlServer数据库的变化
LevelFilter简介说明