日期:2014-05-16 浏览次数:20410 次
代码资源:http://outofmemory.cn/code-snippet/1085/java-usage-JDBC-connection-MYSQL-database
一个简单的访问mysql数据库的main方法。
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestJDBC { /** * 入口函数 * * @param arg */ public static void main(String arg[]) { try { Connection con = null; // 定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); // MYSQL驱动 con = DriverManager .getConnection( "jdbc:mysql://*/liveEpg?useUnicode=true&characterEncoding=UTF-8", "root", "*"); // 链接本地MYSQL Statement stmt; // 创建声明 stmt = con.createStatement(); // 查询数据并输出 String selectSql = "SELECT othChannelId FROM channel_info"; ResultSet selectRes = stmt.executeQuery(selectSql); while (selectRes.next()) { // 循环输出结果集 String username = selectRes.getString("othChannelId"); System.out.println("username:" + username); } } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } } }