这样就可以在Dos窗口下运行了。<br />但还不能在Eclipse 3.2中运行。
要想在Eclipse 3.2下运行,还必须将<br />mysql-connector-java-5.1.0-bin.jar包含到工程中来<br />点击Eclipse 3.2 的Menu下的File菜单,选择Import<br />然后在类型当中选择Archive File<br />然后在From archive file 中选中D:\Program Files\Java\jdk1.5.0_11\mysql-connector-java-5.1.0\mysql-connector-java-5.1.0-bin.jar<br />最后点击完成,即可。
<br />以下为原代码:<br />import java.sql.*;
public class MysqlJdbc {<br /> public static void main(String args[]) {<br /> try {<br /> Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 <br /> //Class.forName("org.gjt.mm.mysql.Driver");<br /> System.out.println("Success loading Mysql Driver!");<br /> }<br /> catch (Exception e) {<br /> System.out.print("Error loading Mysql Driver!");<br /> e.printStackTrace();<br /> }
try {<br /> Connection connect = DriverManager.getConnection(<br /> "jdbc:mysql://localhost:3306/mysql","root","123456789");<br />//连接URL为 jdbc:mysql//服务器地址/数据库名<br />//后面的2个参数分别是登陆用户名和密码<br /> System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();<br /> ResultSet rs = stmt.executeQuery("select * from help_topic");<br /> while (rs.next()) {<br /> System.out.println(rs.getString("name"));
}<br /> }<br /> catch (Exception e) {<br /> System.out.print("get data error!");<br /> e.printStackTrace();<br /> }<br /> }<br />}
<br>
<br>