日期:2014-05-16 浏览次数:20807 次
import java.sql.*; public class SampleIntro { public static void main(String[] args) { try { Connection conn; Statement stmt; ResultSet res; //加载Connector/J驱动 //这一句也可写为:Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver").newInstance(); //建立到MySQL的连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "你的密码"); //执行SQL语句 stmt = conn.createStatement(); res = stmt.executeQuery("select * from pet"); //处理结果集 while (res.next()) { String name = res.getString("name"); System.out.println(name); } res.close(); } catch (Exception ex) { System.out.println("Error : " + ex.toString()); } } }