java连接mysql

时间:2014-04-29 13:33:48   收藏:0   阅读:611
1.下载jar包
网址:http://dev.mysql.com/downloads/
-->点击:"MySQL Connector/J 5.1 — Generally Available (GA) release (recommended)"
-->点击:(mysql-connector-java-5.1.10.zip)后面的download按钮
-->点击:"? No thanks, just take me to the downloads!"
-->随便选一个"HTTP"超链接,开始下载
2.在项目中导入下载的jar包 mysql-connector-java-5.1.10-bin.jar
3.打开mysql
4.编写代码
Java代码 bubuko.com,布布扣 bubuko.com,布布扣
  1.   
  2. package Test;   
  3.   
  4. import java.sql.Connection;   
  5. import java.sql.DriverManager;   
  6. import java.sql.ResultSet;   
  7. import java.sql.Statement;   
  8.   
  9. public class TestMySql   
  10. {   
  11.     public static void main(String[] args)   
  12.     {   
  13.         try  
  14.         {   
  15.             Statement stmt=null;   
  16.             ResultSet rs=null;   
  17.             Class.forName("com.mysql.jdbc.Driver");   
  18.             Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称","root","123456");    
  19.             stmt = (Statement)conn.createStatement();    
  20.             rs = stmt.executeQuery("SELECT * from PetInfo");    
  21.   
  22.             while (rs.next())    
  23.             {    
  24.                  System.out.println(rs.getString(2));   
  25.             }    
  26.         }   
  27.         catch(Exception ex)   
  28.         {   
  29.             ex.printStackTrace();   
  30.         }   
  31.     }   
  32. }  
package Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestMySql
{
	public static void main(String[] args)
	{
		try
		{
			Statement stmt=null;
			ResultSet rs=null;
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称","root","123456"); 
			stmt = (Statement)conn.createStatement(); 
			rs = stmt.executeQuery("SELECT * from PetInfo"); 

			while (rs.next()) 
			{ 
				 System.out.println(rs.getString(2));
			} 
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

 

java连接mysql,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!