JDBC:JAVA连接MySQL数据库成功! 具体见
http://user.qzone.qq.com/274233672/blog/1226809514import java.sql.*; /** * @author qingsongwang * @2008.11.16 * * 说明:实现与数据库相连,取出数据库的内容显示 */ public class JDBCTest { //主函数main() public static void main(String[] args) throws Exception { String kongge=new String(" "); //为后面的结果集输出好看点 Class.forName("com.mysql.jdbc.Driver"); //驱动 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/greatwqs?user=root&password=greatwqs"); /*连接数据库,jdbc:mysql://localhost:3306/greatwqs 数据库为greatwqs数据库 * 端口为3306 * * 用户名user=root * * 用户密码password=greatwqs */ Statement stmt=conn.createStatement(); //创建SQL语句,实现对数据库的操作功能 ResultSet rs=stmt.executeQuery("select * from person"); //返回查询的结果 while(rs.next()) { System.out.print(rs.getString("num")+kongge); System.out.print(rs.getString("name")+kongge); System.out.print(rs.getString("gender")+kongge); System.out.print(rs.getString("major")+kongge); System.out.print(rs.getString("phone")+kongge); System.out.println(); }//输出结果集的内容 rs.close(); stmt.close(); conn.close(); //关闭语句,结果集,数据库的连接. } }