当我们和数据库进行交互的时候都需要一个与数据库相对应的驱动类来进行数据库的连接,无论是SQLServer数据库还是MySql数据库,对应的驱动类是必不可少的,下面就给大家讲一下连接MySql数据库的驱动类。
首先我们必须在项目中导入mysql-connector-java-5.1.39-bin.jar架包
然后就是驱动类的程序了
驱动类代码
package com.zsh.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class DBUtil {public static Connection getConn(){Connection conn = null;//MySql地址String url = "jdbc:mysql://localhost:3306/school";//MySql用户String user = "root";//MySql密码String pwd = "111";try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url, user, pwd);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void closeConn(Connection conn, PreparedStatement ps, ResultSet rs){if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(rs!=null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
了解更多关注我呦!!!