Home >Database >Mysql Tutorial >jdk连接mysql测试_MySQL

jdk连接mysql测试_MySQL

WBOY
WBOYOriginal
2016-06-01 13:45:501501browse

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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn