测试jdbc连mysql数据库_java连接mysql数据库及测试是否连接成功的方法

本文实例讲述了java连接mysql数据库及测试是否连接成功的方法。分享给大家供大家参考,具体如下:

package com.test.tool;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class JDBCUtlTool {

public static Connection getConnection(){

String driver="com.mysql.jdbc.Driver"; //获取mysql数据库的驱动类

String url="jdbc:mysql://localhost:3306/test"; //连接数据库(kucun是数据库名)

String name="root";//连接mysql的用户名

String pwd="123456";//连接mysql的密码

try{

Class.forName(driver);

Connection conn=DriverManager.getConnection(url,name,pwd);//获取连接对象

return conn;

}catch(ClassNotFoundException e){

e.printStackTrace();

return null;

}catch(SQLException e){

e.printStackTrace();

return null;

}

}

public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){

try{

if(rs!=null){

rs.close();

}

}catch(SQLException e){

e.printStackTrace();

}

try{

if(ps!=null){

ps.close();

}

}catch(SQLException e){

e.printStackTrace();

}

try{

if(conn!=null){

conn.close();

}

}catch(SQLException e){

e.printStackTrace();

}

}

public static void main(String[] args) throws SQLException

{

Connection cc=JDBCUtlTool.getConnection();

if(!cc.isClosed())

System.out.println("Succeeded connecting to the Database!");

Statement statement = cc.createStatement();

String sql = "select * from test2";

ResultSet rs = statement.executeQuery(sql);

while(rs.next()) {

System.out.println(rs.getString("id")+"");

}

}

}

更多关于java相关内容感兴趣的读者可查看本站专题:《Java+MySQL数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

本文标题: java连接mysql数据库及测试是否连接成功的方法

本文地址: http://www.cppcns.com/ruanjian/java/202264.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440581.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql配置日志老化配置_mysql配置-日志大小限制和自动删除

线上的项目磁盘消耗问题, 发现和MySQL日志有关系.需要处理的问题如何限制大小 不让日志无限膨胀?配置日志不留?删除的方式和直接删除会对服务有什么影响?解决方式限制大小, 保留最近一段时间日志.set global expire_logs_days7; # 命令行进入MySQL中, 临时设置保留最近7天日…

【HDU - 5091】Beam Cannon(线段树,扫描线)

题干: Recently, the γ galaxies broke out Star Wars. Each planet is warring for resources. In the Star Wars, Planet X is under attack by other planets. Now, a large wave of enemy spaceships is approaching. There is a very large Beam Cannon on t…

mysql的传播特性_spring事务传播特性和mysql事务隔离级别

spring事务的传播特性--7种REQUIRED支持当前事务,如果没有事务会创建一个新的事务SUPPORTS支持当前事务,如果没有事务的话以非事务方式执行MANDATORY(强制性)支持当前事务,如果没有事务抛出异常REQUIRES_NEW创建一个新的事物并挂起当前事务NO…

【 HDU - 5093】Battle ships(匈牙利算法,二分图匹配)

题干: Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently. Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both si…

【HDU - 5090】Game with Pearls (匈牙利算法,二分图匹配)

题干: Tom and Jerry are playing a game with tubes and pearls. The rule of the game is: 1) Tom and Jerry come up together with a number K. 2) Tom provides N tubes. Within each tube, there are several pearls. The number of pearls in each tube i…

qt同时连接oracle和mysql_QT连接Oracle和Mysql的详细步骤,已成功~!

近几天一直在整QT的数据库连接这一块。因为QT是开源的,所以涉及的连接Oracle及Mysql的驱动都必须自己编译生成。通过不断的测试、调试,终于把QT连接Oracle和Mysql的驱动编译生成好了。QT环境:Qt 4.6.0打开Qt Command Prompt,开始菜…

【POJ - 2632】Crashing Robots(模拟)

题干: In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occup…

一台linux上运行多个mysql_linux下同时运行多个mysql

来自网络,感谢开源,感谢分享通过rpm安装mysql,测试版本5.1.481、在linux下通过:#useradd multi -g mysql -s /sbin/nologin添加一个multi用户,并加入到mysql组#passwd multi给multi用户添加密码,这里设置的密码为multi…

【SPOJ - QTREE2】Query on a tree II(LCA,倍增)

题干: You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length. We will ask you to perfrom some instructions of the foll…

pandownload用户未登录_Pandownload再度复活,下载速度飙升到10MB/s以上

PanDownload再度复活版,此版镜像服务器由新城旧梦维护卢本伟修改版,基本都是维持在10M/S的下载速度,如果你的宽带够大,下载速度会更快,无需安装即可免费使用,但是需要登陆才能下载。(Pandownload卢本伟修改…

【蓝桥杯 - 试题】立方尾不变(tricks,快速取出一个数字的后n位)

题干: 有些数字的立方的末尾正好是该数字本身。 比如:1,4,5,6,9,24,25,.... 请你计算一下,在10000以内的数字中(指该数字,并非它立方后的数值),符合这个特征的正整数一共有多少个。 请提交该…

momentjs转换格式_Moment.js+Vue过滤器的使用,各种时间格式转换为YYYY-MM-DD HH:mm:ss格式...

前言这篇文章将Moment.js与vue过滤器连用。如果不会过滤器的朋友,可以先看这篇文章vue过滤器一、Moment.js是什么?Moment.js是JavaScript 日期处理类库。使用场景:vue项目中经常需要将时间戳转换为各种时间格式再显示。二、使用步骤1.安装这里…

【HDU - 1943】Ball bearings(几何问题)

题干: The Swedish company SKF makes ball bearings. As explained by Britannica Online, a ball bearing is “one of the two types of rolling, or anti friction, bearings (the other is the roller bearing). Its function is to connect two machine mem…

mysql显示修改密码_MySQL修改密码

第一种方式:最简单的方法就是借助第三方工具Navicat for MySQL来修改,方法如下:1、登录mysql到指定库,如:登录到test库。2、然后点击上方“用户”按钮。3、选择要更改的用户名,然后点击上方的“编辑用户”按…

【POJ - 2486】Apple Tree (树形背包,dp)

题干: Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the no…

mysql 磁盘组_有效管理 ASM 磁盘组空间

ORA-15041: diskgroup space exhausted 对您的数据库环境的直接和间接影响?与 ASM 磁盘组相关的磁盘空间问题和 ORA-15041 错误会ORA-15041: diskgroup space exhausted 对您的数据库环境的直接和间接影响?与 ASM 磁盘组相关的磁盘空间问题和 ORA-15041 …

【HDU - 1561】The more, The Better(树形背包,dp,依赖背包问题与空间优化,tricks)

题干: ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物。但由于地理位置原因,有些城堡不能直接攻克,要攻克这些…

mysql判断域为空_MySQL EXPLAIN 字段说明

id查询或关联查询的顺序。如果没有子查询且只有一个查询,则为一个常数 1,表示第一步;如果有子查询,则子查询为 1,父查询为 2;相同的 id 查询顺序为自上而下;如果有子查询,不同 id 值…

【CodeForces - 618A】Slime Combining(二进制,思维)

题干: Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1. You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other…

mysql索引技术_MySQL索引类型

首先请查看不同引擎支持的索引类型:存储引擎简介 。聚集索引和非聚集索引概念见:聚集索引与非聚集索引 和 聚集索引 。 覆盖索引见:覆盖索引 。1. InnoDB的每一个表都会有一个聚集索引(第一索引,主键索引)。InnoDB按照主键进行聚集…