java中jdbc驱动连接mysql获取数据的例子

发布时间:2019-08-01编辑:脚本学堂
java中jdbc驱动连接mysql并输出数据的例子,java编程中jdbc数据库驱动的用法举例。

完整代码:
 

复制代码 代码示例:

package cn.outofmemory.snippets.core;

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

public class SelectRowsExample {
public static void main(String[] args) {
Connection connection = null;

try {
// Load the mysql JDBC driver
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);

// Create a connection to the database
String serverName = "localhost";
String schema = "test";
String url = "jdbc:mysql://" + serverName +"/" + schema;
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);

System.out.println("Successfully Connected to the database!");
}
catch (ClassNotFoundException e) {
   System.out.println("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
   System.out.println("Could not connect to the database " + e.getMessage());
}

try {
// Get a result set containing all data from test_table
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM test_table");

// For each row of the result set ...
while (results.next()) {

// Get the data from the current row using the column index - column data are in the VARCHAR format
String data = results.getString(1);
System.out.println("Fetching data by column index for row " + results.getRow() + " : " + data);

// Get the data from the current row using the column name - column data are in the VARCHAR format
data = results.getString("test_col");
System.out.println("Fetching data by column name for row " + results.getRow() + " : " + data);

}

/*
* Please note :
* ResultSet API provides appropriate methods for retrieving data
* based on each column data type e.g.
*
* boolean bool = rs.getBoolean("test_col");
* byte b = rs.getByte("test_col");
* short s = rs.getShort("test_col");
* int i = rs.getInt("test_col");
* long l = rs.getLong("test_col");
* float f = rs.getFloat("test_col");
* double d = rs.getDouble("test_col");
* BigDecimal bd = rs.getBigDecimal("test_col");
* String str = rs.getString("test_col");
* Date date = rs.getDate("test_col");
* Time t = rs.getTime("test_col");
* Timestamp ts = rs.getTimestamp("test_col");
* InputStream ais = rs.getAsciiStream("test_col");
* InputStream bis = rs.getBinaryStream("test_col");
* Blob blob = rs.getBlob("test_col");
*/

} catch (SQLException e) {
System.out.println("Could not retrieve data from the database " + e.getMessage());
}

}
}

输出结果:
 

Successfully Connected to the database!
Fetching data by column index for row 1 : new_test_value
Fetching data by column name for row 1 : new_test_value
Fetching data by column index for row 2 : new_test_value_0
Fetching data by column name for row 2 : new_test_value_0
Fetching data by column index for row 3 : new_test_value_1
Fetching data by column name for row 3 : new_test_value_1
Fetching data by column index for row 4 : new_test_value_2
Fetching data by column name for row 4 : new_test_value_2
Fetching data by column index for row 5 : new_test_value_3
Fetching data by column name for row 5 : new_test_value_3
Fetching data by column index for row 6 : new_test_value_4
Fetching data by column name for row 6 : new_test_value_4
Fetching data by column index for row 7 : new_test_value_5
Fetching data by column name for row 7 : new_test_value_5
Fetching data by column index for row 8 : new_test_value_6
Fetching data by column name for row 8 : new_test_value_6
Fetching data by column index for row 9 : new_test_value_7
Fetching data by column name for row 9 : new_test_value_7
Fetching data by column index for row 10 : new_test_value_8
Fetching data by column name for row 10 : new_test_value_8
Fetching data by column index for row 11 : new_test_value_9
Fetching data by column name for row 11 : new_test_value_9