Mysql的JDBC

  Java程序可以通过JDBC链接数据库,通过JDBC可以方便的访问数据库,不必为特定的数据库编写专门的程序。

  需要先配置mysql-connector-java-5.0.8-bin.jar

  使用JDBC连接数据库的开发流程如下:

  1. 注册数据库驱动  Class.forName("com.mysql.jdbc.Driver");
  2. 构建数据库连接的URL,Mysql的连接URL为"jdbc:mysql://localhost:3306/test"
  3. 获取Connection对象,该对象是JDBC封装的数据库连接对象,只有创建此对象后,才可以对数据执行相关操作。DriverManager.getConnection(url, username, password);

     如下为JDBC核心API的五个接口:

例:mysql数据表格式:

增加操作的核心代码:

try{
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost:3306/student";
    Connection con=DriverManager.getConnection(url, "root", "");
    String sql="insert into tb_books(name, price, bookCount, author) values(?,?,?,?)";
    PreparedStatement ps=con.prepareStatement(sql);
    ps.setString(1, book.getName());
    ps.setDouble(2, book.getPrice());
    ps.setInt(3, book.getBookCount());
    ps.setString(4, book.getAuthor());
    int row = ps.executeUpdate();
    if(row>0){
        out.print("成功添加"+row+"  行数据");
    }
    ps.close();
    con.close();
}
catch(Exception e){
    out.print("添加失败!");
    e.printStackTrace();
}

查询操作的核心代码:

        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/student";
            Connection con=DriverManager.getConnection(url, "root", "");
            Statement statement=con.createStatement();
            String sql="select * from tb_books";
            ResultSet rs=statement.executeQuery(sql);
            ArrayList<Book> list=new ArrayList<Book>();
            while(rs.next()){
                Book book=new Book();
                book.setName(rs.getString("name"));
                book.setPrice(rs.getDouble("price"));
                book.setBookCount(rs.getInt("bookCount"));
                book.setAuthor(rs.getString("author"));
                list.add(book);
            }
            request.setAttribute("list", list);
            rs.close();
            con.close();
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        catch(SQLException e){
            e.printStackTrace();
        }

修改数据的核心代码:

try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/student";
            Connection con=DriverManager.getConnection(url, "root", "");
            String sql="update tb_books set bookCount=? where name=?";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setInt(1, bookCount);
            ps.setString(2, name);
            ps.executeUpdate();
            ps.close();con.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

对数据的其他操作类似。

      

时间: 2024-11-02 11:04:20

Mysql的JDBC的相关文章

mysql的JDBC接口编程

mysql的JDBC接口驱动包的版本和mysql的版本间的关系可以在官网上查到,具体例子见下面,里面没有和JDBC版本匹配的相关描述. http://dev.mysql.com/doc/relnotes/connector-j/en/news-5-1-34.html Version 5.1.34 is a maintenance release of the production 5.1 branch. It is suitable for use with MySQL server versi

Apache Tomcat/6.0.39如何配置连接mysql,JDBC:mysql-connector-java-5.1.30-bin.jar-成功连接心得

http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html 前提:开启TOMCAT,MYsql MySQL DBCP Example0. Introduction Versions of MySQL and JDBC drivers that have been reported to work: MySQL 3.23.47, MySQL 3.23.47 using InnoDB,, MySQL 3.23

关于Eclispse连接Mysql的Jdbc

1.在Eclipse中新建Java工程 2.引入JDBC库(在bulid path 的extenrnal里) 3. 1)导入sql包(import java.sql.*) 2)加载(注册)mysql jdbc驱动程序 Class.forName("com.mysql.jdbc.Driver"); 3)连接数据库(url) Connection connect=DriverManager.getConnection("jdbc:mysql://localhost:3306/te

com.mysql.jdbc.Driver ==》 com.mysql.cj.jdbc.Driver

错误信息: SqlSession [[email protected]] was not registered for synchronization because synchronization is not active Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatical

java.lang.UnsupportedClassVersionError: com/mysql/cj/jdbc/Driver : Unsupported major.minor version 5

原因: com/mysql/cj/jdbc/Driver是6.0版本的驱动,兼容JDK8环境,不兼容JDK7环境,在基于jdk7的tomcat中编译运行会出错,在基于jdk8的tomcat中编译运行则不会出错. 解决方案: 1.jdk7+老版5.0驱动com/mysql/jdbc/Driver 2.jdk8+新版6.0驱动com/mysql/cj/jdbc/Driver 原文地址:https://www.cnblogs.com/GH0522/p/9328416.html

Loading class `com.mysql.jdbc.Driver&#39;. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver&#39;.

在连接数据库时,使用了最新版本的mysql-Connector,所以导致老版本的"com.mysql.jdbc.Drive"不可行,要改为"com.mysql.cj.jdbc.Driver" Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. 原文地址:https://www.cnblogs.com

MyBatis Generator使用com.mysql.cj.jdbc.Driver遇到的问题

MyBatis Generator使用com.mysql.cj.jdbc.Driver Mybatis Generator 1.3.5 新建了一个decision库,并创建了一张user表 import org.apache.commons.lang3.StringUtils; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.*; import org.mybatis.g

com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别

调试代码时,发现错误: Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 在网上巴拉巴拉发现,原

关于MySql升级JDBC架包导致时区问题报错(The server time zone value &#39;?й???????&#39; is unrecognized or represents more than one time zone)

报错信息: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want

mysql的驱动类com.mysql.jdbc.Driver过时了,需要用com.mysql.cj.jdbc.Driver代替

springboot项目整合mybatis,配置文件如下: server: port: 8081 mybatis: config-location: classpath:mybatis/mybatis-config.xml #mybatis配置文件所在路径 type-aliases-package: com.yuanqiao.entities #所有entity别名类所在包 mapper-locations: classpath:mybatis/mapper/*.xml spring: appl