標籤:127.0.0.1 roo etc exec manager demo1 執行sql 語句 技術分享
簡單地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");//操作ResultSetwhile(resultSet.next()){System.out.println(resultSet.getString("cust_name"));}//關閉資料庫連接resultSet.close();statement.close();connection.close();}}
這是MySQL表
串連成功,擷取表資料:
Java JDBC串連MySQL