java基础连接数据库进行查询,java数据库,package cn.o
分享于 点击 5989 次 点评:156
java基础连接数据库进行查询,java数据库,package cn.o
package cn.outofmemory.snippets.core;import org.apache.commons.dbcp.BasicDataSource;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class SimpleBasicDataSource { public static void main(String[] args) throws Exception { // Create a BasicDataSource object and configure database BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost/testdb"); dataSource.setUsername("root"); dataSource.setPassword("root"); Connection conn = null; PreparedStatement stmt = null; try { // Get connection and execute a simple query conn = dataSource.getConnection(); stmt = conn.prepareStatement("SELECT * FROM users"); ResultSet rs = stmt.executeQuery(); // Print fetched data while (rs.next()) { System.out.println("Username : " + rs.getString("username")); } } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } }}
输出:
Username : ByronUsername : IliasUsername : NikosUsername : Dimitris
用户点评