ITPub博客

首页 > 数据库 > MySQL > Eclipse connect to  Mysql

Eclipse connect to  Mysql

MySQL 作者:CCTV少儿 时间:2014-03-09 16:02:05 0 删除 编辑
(The follow option is after you had installed the  mysql and configure the related environment)
Project->properties->Java Build Path->Libraries->Choose Add External,
Choose the mysql-connector-java-5.1.13-bin.jar

package my;
import java.sql.*;
public class MysqlJdbc{
    public static void main(String args[]) {
        String sqlDriver = "com.mysql.jdbc.Driver";
        Connection connect = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序    
            System.out.println("Success loading Mysql Driver!");
        }
        catch (Exception e) {
            System.out.print("Error loading Mysql Driver!");
            e.printStackTrace();
        }
        try {
            String url = "jdbc:mysql://localhost:3306/hope?user=root&password=xiu1314";
            Class.forName(sqlDriver).newInstance();
            connect = DriverManager.getConnection(url,"root","password"); 
                         //password is that you set when u install the mysq
            stmt = connect.createStatement();
            rs = stmt.executeQuery("select * from comment");
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
        }
        catch (Exception e) {
             System.out.print("get data error!");
             e.printStackTrace();
        }
      }
}
<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23578877/viewspace-1114539/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-03-23