• java连接mysql


    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代码 复制代码 收藏代码
    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();
    		}
    	}
    }
    
    
     
  • 相关阅读:
    hadoop之 distcp(分布式拷贝)
    LINUX下mysql客户端不能输入中文
    logstash-input-jdbc配置说明
    数据收集之binlog同步 Maxwell --->Kafka
    MySQL的binlog日志
    SqlServer字段说明查询及快速查看表结构
    【HTTP】C1-Web及网络基础
    第 2 期:['1', '2', '3'].map(parseInt) what & why
    用表单处理用户输入
    德国Vue.js2终极开发教程033--
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/3698439.html
Copyright © 2020-2023  润新知