java-一个简单的访问DB的main方法使用
代码资源: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", "*"); // 链接本地MYSQLStatement 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());}}}