设为首页 加入收藏

TOP

jdk连接mysql测试
2014-11-24 07:34:01 来源: 作者: 【 】 浏览:0
Tags:jdk 连接 mysql 测试

  

  import java.sql.*;
public class TestJDBC {
public static void main(String[] arges) throws SQLException,ClassNotFoundException{
String user="root";
String pwd="mysql";
//设定URL
String myjdbc="jdbc:mysql://localhost:3306/test";
//加载MYSQL驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接会话
Connection myConnection=DriverManager.getConnection(myjdbc,user,pwd);
//创建语句块对象
Statement Myoperation=myConnection.createStatement();
//获取结果集对象
ResultSet record=Myoperation.executeQuery("select *from test");
while(record.next()){
//输出结果集对象
System.out.println(record.getInt("id")+","+record.getString("name"));
}
//关闭结果集
try{
if(record!=null)
//关闭结果集时需要处理异常
record.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(myConnection!=null)
myConnection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}

本文出自 “java技术社区” 博客

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇解决mysql连接过慢的方法 下一篇centos下MySQL 5.5.11 CMake安装..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)
·[ Linux运维学习 ] (2025-12-26 02:52:27)
·HTTPS 详解一:附带 (2025-12-26 02:20:37)
·TCP/IP协议到底在讲 (2025-12-26 02:20:34)