关于myeclipse与mysql连接问题?

[复制链接]
查看11 | 回复3 | 2011-5-11 18:04:12 | 显示全部楼层 |阅读模式
import java.sql.*;//导入包
import java.util.*;
        public class Main{
                public static void main(String arg[]){
                String driver=null;// 驱动程序名称
                //指定连接的数据库url
                ResultSet rs=null;
                Statement stmt=null;
                Connection conn=null;
                ArrayList a;
                try{
                Class.forName("org.gjt.mm.mysql.Driver");
                String url ="jdbc:mysql://localhost/5.11?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1";
                //myDB为数据库名
               
                 conn= DriverManager.getConnection(url);
                        //创建执行查询的Statement对象
                         stmt = conn.createStatement();
                        //SQL语句,用于查询学生表中的姓名、年龄、和姓别
                        String sql = "SELECT name,id,sex FROM person";
                        //执行查询,查询结果放在ResultSet的对象中
                        rs = stmt.executeQuery( sql );
                        String name,sex;
                        int id;
                        //打印出查询结果。
                        while(rs.next()){
                                //获得每一行中每一列的数据
                                name = rs.getString(1);                       
                                id = rs.getInt(2);
                                sex = rs.getString(3);
                       
System.out.println(name + ", " + id+", " +sex);
                                }
                        }
                //找不到驱动程序,捕捉异常。如发生该错误,
                //请检查JDK版本是不是在1.1以上
                catch(ClassNotFoundException e){
                        System.out.println("错误:" + e);
                }
                catch(SQLException e1){
                        System.out.println("错误:" + e1);
                }
                finally{
                        try{
                                rs.close();
                                stmt.close();
                                conn.close();
                        }
                        catch(SQLException e2){}
                }
                }
        }
错误:java.sql.SQLException: Access denied for user 'soft'@'localhost' (using password: YES)
Exception in thread "main" java.lang.NullPointerException
        at Main.main(Main.java:45)
这些错误是什么意思?

回复

使用道具 举报

千问 | 2011-5-11 18:04:12 | 显示全部楼层
你的mysql没有soft这个用户,你在mysql数据库中建立这么个用户就可以了! sqlyog这个工具很好用的!
回复

使用道具 举报

千问 | 2011-5-11 18:04:12 | 显示全部楼层
>jdbc:mysql://localhost/5.115.11这个DB存在吗?请确认
回复

使用道具 举报

千问 | 2011-5-11 18:04:12 | 显示全部楼层
在45行有个空指针异常
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行