zoukankan      html  css  js  c++  java
  • Java JDBC连接MySQL

        简单地JDBC小实例

    package com.javaJDBCTest;
    
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    
    public class JDBCTestDemo1 {
    
    	public static void main(String args[]) throws Exception
    	{
    		//加载驱动类
    		Class.forName("com.mysql.jdbc.Driver");
    		
    		//通过DriverManager获取数据库连接
    		String url = "jdbc:mysql://127.0.0.1/TestDB";
    		String user = "root";
    		String password = "ddddd";
    		Connection connection = (Connection) DriverManager.getConnection(url, user, password);
    		
    		//通过connection对象获取Statement对象,执行SQL语句
    		Statement statement = (Statement)connection.createStatement();
    		ResultSet resultSet = statement.executeQuery("select * from customers");
    		
    		//操作ResultSet
    		while(resultSet.next())
    		{
    			System.out.println(resultSet.getString("cust_name"));
    		}
    		
    		//关闭数据库连接
    		resultSet.close();
    		statement.close();
    		connection.close();
    	}
    }
    

     这是MySQL表

     连接成功,获取表数据:

     

  • 相关阅读:
    Close doc
    Add to word and save
    xml dataGrideView
    listView save and get
    extract word into string
    DataGrideView DataTable XML
    bookmark
    VB对象的链接与嵌入
    文件系统处理(一)
    既然选择了远方,便只顾风雨兼程
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6954081.html
Copyright © 2011-2022 走看看