• 技术文章 >数据库 >mysql教程

    jdk连接mysql测试_MySQL

    2016-06-01 13:45:50原创612
    bitsCN.com

      

      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技术社区” 博客

    bitsCN.com
    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:mysql 测试
    上一篇:JBoss AS7配置MySQL数据源_MySQL 下一篇:局域网内连接MySQL_MySQL
    大前端线上培训班

    相关文章推荐

    • 全面讲解MySQL8.0 For Windows的安装方法• MySQL中什么是索引?索引存储模型浅析• 分析MySQL用户中的百分号%是否包含localhost?• 聊聊mysql的cmake方式• 步骤分明地教你在MAC上安装MYSQL

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网