欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

jdbc连接mysql获得数据,jdbcmysql获得,package cn.o

来源: javaer 分享于  点击 40058 次 点评:21

jdbc连接mysql获得数据,jdbcmysql获得,package cn.o


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());    }  }}

Example Output:

Successfully Connected to the database!Fetching data by column index for row 1 : new_test_valueFetching data by column name for row 1 : new_test_valueFetching data by column index for row 2 : new_test_value_0Fetching data by column name for row 2 : new_test_value_0Fetching data by column index for row 3 : new_test_value_1Fetching data by column name for row 3 : new_test_value_1Fetching data by column index for row 4 : new_test_value_2Fetching data by column name for row 4 : new_test_value_2Fetching data by column index for row 5 : new_test_value_3Fetching data by column name for row 5 : new_test_value_3Fetching data by column index for row 6 : new_test_value_4Fetching data by column name for row 6 : new_test_value_4Fetching data by column index for row 7 : new_test_value_5Fetching data by column name for row 7 : new_test_value_5Fetching data by column index for row 8 : new_test_value_6Fetching data by column name for row 8 : new_test_value_6Fetching data by column index for row 9 : new_test_value_7Fetching data by column name for row 9 : new_test_value_7Fetching data by column index for row 10 : new_test_value_8Fetching data by column name for row 10 : new_test_value_8Fetching data by column index for row 11 : new_test_value_9Fetching data by column name for row 11 : new_test_value_9
相关栏目:

用户点评