在这个class里写入
package mysqlconnect; import java.sql.*; public class mysqlconnect { public static void main(String[] args) { //加载数据库驱动 try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //设置用户信息和url String user = "root"; String password = "123456"; /*密码*/ String url = "jdbc:mysql://localhost:3306/student"; /*jdbc:mysql://localhost:3306/数据库中的库名*/ //获取数据库对象connection Connection conn = null; try { conn = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //通过数据库对象connection获取statement对象 Statement stat = null; try { stat = conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //执行sql语句 String sql = "insert into admit values ('123','123')"; /*为student数据库里admit表加值*/ try { stat.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //释放连接资源 try { stat.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
最后不会报错就大功告成了
总结
到此这篇关于java最新版本连接mysql失败的文章就介绍到这了,更多相关java连接mysql失败内容请搜索
更多Mysql内容来自木庄网络博客
标签:Mysql
相关阅读 >>
mysql导入大批量数据出现mysql server has gone away的解决方法
mysql的left join、right join、inner join
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。