标签:
package com.simope.test; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class ConnToMySql { public static final String url = "jdbc:mysql://localhost/my_db"; public static final String userName = "root"; public static final String password = "root"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = (Connection) DriverManager.getConnection(url, userName, password); stmt = (Statement) conn.createStatement(); String sql = "select * from sys_user"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println("按照列下标:" + rs.getInt(1)); System.out.println("按照列名:" + rs.getInt("userID")); } } catch (Exception e) { System.out.println("first:" + e); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { System.out.println("second:" + e); } } } }
所用到的jar包:mysql-connector-java-5.1.5-bin.jar
标签:
原文地址:http://www.cnblogs.com/JimLy-BUG/p/5216929.html