首页 > 其他分享 >JDBC

JDBC

时间:2022-12-17 22:45:07浏览次数:34  
标签:JDBC String prepareStatement sql preparedStatement SQL

JDBC:java连接数据库

固定步骤

加载驱动
连接数据库
向数据库发送SQL的对象Statement:CRUD
编写SQL(根据业务编写不同的SQL语句)
执行SQL
关闭连接
注:在数据库的编写之中尽量的采用prepareStatement

package com.li.Dao;


import java.sql.*;

public class Daodemo0 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
String root="root";
String password="lyj18366635303";
String driver = "com.mysql.cj.jdbc.Driver";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url,root,password);
// String sql="select * from class";
String sql="insert into class (categoryId,pId,categoryName) values(?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,12);
preparedStatement.setInt(2,12);
preparedStatement.setString(3,"web");
int i = preparedStatement.executeUpdate();

if (i>0){
System.out.println("插入成功");
}
preparedStatement.close();
connection.close();

}
}



注:在使用statement与PrepareSatatement的区别

Statement
int i = preparedStatement.executeUpdate(sql);
prepareStatement
int i = preparedStatement.executeUpdate();

标签:JDBC,String,prepareStatement,sql,preparedStatement,SQL
From: https://www.cnblogs.com/DREAM2021/p/16989758.html

相关文章

  • MVC、三层架构、数据库连接池、Spring JDBC
    MVC模式MVC全名是ModelViewController,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻......
  • 关于PB用JDBC连接MySQL,亲测有效
        以前自学过一段时间的PB,数据窗口让人印象深刻,前段时间,在西瓜视频看到有人录制了PB的教学视频,让我想起以前自学的那段时光,遇到了问题,也不知道问谁,现在网......
  • JDBC_API详解
    DiverManager(驱动管理类)作用:①注册驱动②获取数据库链接Connection(数据库连接对象)作用:①获取执行sql的对象②管理事务获取SQL对象事务管理......
  • JDBC入门
    JDBC快速入门URL路径语法jdbc:mysql://ip地址(域名):端口号/数据库名称如果连接的是本机mysql服务器,并且mysql服务器默认端口是3306,则url可以缩写为:jdbc:mysql:///数......
  • maven缺失ojdbc6解决方法(手动安装ojdbc6)
    maven缺失ojdbc6解决方法(手动安装ojdbc6)1.首先下载ojdbc6jar包jar下载地址一(需登录)jar下载地址二(直接下载)2.进入到jar包所在文件夹,执行cmd命令cmd终端执行下面......
  • ssm报错Could not open JDBC Connection for transaction; nested exception is com.m
    HTTPStatus500-Requestprocessingfailed;nestedexceptionisorg.springframework.transaction.CannotCreateTransactionException:CouldnotopenJDBCConnecti......
  • 基于Sharding-Jdbc 实现的读写分离实现
    1.pom文件依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version......
  • jdbc中druid连接池遇到的问题和jdbcTemplate
    无效的源发行版11这是jdk版本不一致,去项目结构里排查一下严重:initdatasourceerrorcom.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:Coulj......
  • JDBC
    编写代码步骤创建工程,导入驱动jar包注册驱动Class.forName("com.mysql.jdbc.Driver");获取连接Connectionconn=DriverManager.getConnection(url,usernam......
  • 使用线程池和shardingsphere-jdbc对统计进行分表查询优化
    记录之前的一次优化过程,之前发布在wiki上,现摘出发布。0.前言主要查询表为还款计划表xx_plan(近4000w,日新增10~20w)、实还记录表xx_actual(2600w+,日新增5~10w)、代偿记录表x......