java测试mysql连接_利⽤java连接mysql数据库时怎么测试是
否连接成功
利⽤java连接mysql数据库时怎么测试是否连接成功
发布时间:2020-12-03 16:06:00
来源:亿速云
阅读:80
作者:Leah
本篇⽂章为⼤家展⽰了利⽤java连接mysql数据库时怎么测试是否连接成功,内容简明扼要并且容易理解,绝对能使你眼前⼀亮,通过这篇⽂章的详细介绍希望你能有所收获。
具体⽅法如下:
st.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="sql.jdbc.Driver"; //获取mysql数据库的驱动类
String url="jdbc:mysql://localhost:3306/test"; //连接数据库(kucun是数据库名)
String name="root";//连接mysql的⽤户名
String pwd="123456";//连接mysql的密码
try{
Class.forName(driver);
Connection Connection(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();
sql容易学吗}
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 Connection();
if(!cc.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = cc.createStatement();
String sql = "select * from test2";
ResultSet rs = uteQuery(sql);
()) {
System.out.String("id")+"");
}
}
上述内容就是利⽤java连接mysql数据库时怎么测试是否连接成功,你们学到知识或技能了吗?如果还想学到更多技能或者丰富⾃⼰的知识储备,欢迎关注亿速云⾏业资讯频道。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论