Java学习-12 JDBC 连接mysql
2018-11-02 22:18
197 查看
步骤一:引入jar包 MySQL官网下载
后在eclipse中找到项目文件后右击后bulidpath最后添加到library,或者直接复制jar到文件中右击addtobulidpath
步骤二 : 通过Java反射加载jar包中的 driver.class 驱动
(下面jar包均为8.0版本)
[code] Class.forName("com.mysql.cj.jdbc.Driver");// 加载具体的驱动类
步骤三:与数据库建立连接
a:定义连接参数,URL尤其需重视
[code] private static final String URL = "jdbc:mysql://localhost:3306/imooc?characterEncoding=utf8&useSSL=true&serverTimezone=UTC&autoReconnect=true"; private static final String USERNAME = "root"; private static final String PWD = "123123";
b:连接
[code] connection = DriverManager.getConnection(URL, USERNAME, PWD); stmt = connection.createStatement();
步骤四:发送sql语句,sql
步骤五 :操作MySQL
[code] int count = stmt.executeUpdate(sql);
例子
[code]import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class sqlTest { public static void main(String[] args) { String driverName = "com.mysql.cj.jdbc.Driver"; String URL="jdbc:mysql://127.0.0.1:3306/imooc?characterEncoding=utf8&useSSL=true&serverTimezone=UTC"; final String USER = "root"; final String PWD = "123123"; Statement stmt = null; Connection conn = null; try{ Class.forName(driverName); conn = DriverManager.getConnection(URL,USER,PWD); stmt = conn.createStatement(); String sql = "update student set STUNAME='lasdasds' where stuno=2"; int count = stmt.executeUpdate(sql); System.out.println(count); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } finally{ try{ stmt.close(); conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
阅读更多
相关文章推荐
- JAVA学习笔记-JAVA用JDBC连接MySQL
- Java学习笔记——JDBC之与数据库MySQL的连接以及增删改查等操作
- 【Java学习笔记】54:CentOS下安装MySQL的JDBC驱动并尝试连接
- (韩顺平讲解)jdbc学习(四)---java连接mysql
- Java学习笔记之<JDBC连接MySQL的步骤>
- JDBC学习之八:JDBC连接MySQL
- Java基础代码库:JDBC连接MySQL
- Java 学习笔记01:使用JDBC连接MYSQL数据库
- 使用jdbc连接mysql 出现 java.sql.Exception: this specifer does...(root@'%')..doesn't exist.
- java中利用JDBC连接MySQL方法总结
- 简单好用! JAVA JDBC 连接 MYSQL
- JAVA中用JDBC连接mysql
- java学习:jdbc连接示例
- 用JDBC,Java连接MySQL并使用prepareStatement
- 简单好用! JAVA JDBC 连接 MYSQL
- java学习:weblogic下JNDI及JDBC连接测试(weblogic环境)
- 编写操作数据库的JAVA程序时需要的连接MySQL数据库的JDBC连接包mysql-connector-java-5.1.10.zip怎么安装
- Java实现Mysql的jdbc连接例子
- Linux下Java连接Mysql 出现“ClassNotFoundException:com.mysql.jdbc.Driver”
- Java与Mysql6的jdbc连接例子