网上差了点资料,按照说明照着做了,JSP运行没问题,但是运行JAVA程序的话就问题来了

解决方案 »

  1.   

    package pag;
    import java.sql.*;
    public class conn{
    String driverName="com.mysql.jdbc.Driver";
    String connect="jdbc:mysql://localhost/fdb_product?user=root";
    String userName="root";
    String password="";
    Connection con=null;
    ResultSet rs=null;
    public void con(){
    try{
    Class.forName(driverName);
    }catch(java.lang.ClassNotFoundException e){
    System.err.println(e.getMessage());
    }
    }

    public ResultSet executeQuery(String sql){
    try{
    con=DriverManager.getConnection(connect);
    Statement stmt=con.createStatement();
    rs=stmt.executeQuery(sql);
    }catch(SQLException ex){
    System.err.println(ex.getMessage());
    }
    return rs;
    }
    }
      

  2.   

    String connect="jdbc:mysql://localhost/fdb_product?user=root";
    ????
    ------------------------------------------------------------------
    String userName="root";
    String password="";
    ------------------------------------------------------------------
    看出来了吧
      

  3.   

    com.mysql.jdbc.Driver是不合法的驱动
     PoemCode(诗意代码之魔数)所说的是正确的,我在JSP里面已经可以完成。但是在JAVA程序里面就会报没有找到合适的驱动。这是为什么?我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。
      

  4.   

    /*
     * SQLController.java
     *
     * Created on 2006年1月16日, 下午1:33
     */package sqlmvc;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Vector;/**
     *
     * @author poco
     */
    public class SQLController {
        
        /** Creates a new instance of SQLController */
        public SQLController() {
        }
        
        public Connection con;
        
        public DriverManager driver;
        
        public Statement state;
        
        public Connection getCon() {
            if(con == null){
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    System.out.println("load the driver");
                    con = DriverManager.getConnection("jdbc:mysql://localhost/MVC?user=poemcode&password=aaaa&useUnicode=true&characterEncoding=UTF-8");
                    System.out.println("try to get Connetion");
                    if(con == null)
                        System.out.println("failed to get Connection.");
                    else
                        System.out.println("get Conection");
                } catch (ClassNotFoundException ex) {
                    System.err.println("Can't find the driver of MySql");
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    System.out.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
                    ex.printStackTrace();
                }
            }
            return con;
        }
        
        public Vector cache;
        
        public int colCount;
        
        public String[] headers;
        
        public void doQuery(String info) {
            cache = new Vector();
            try {
                state = getCon().createStatement();
                System.out.println("create Statement");
                result = state.executeQuery(info);
                System.out.println("get ResultSet");
                if(state.execute(info)){
                    result = state.getResultSet();
                    rsmd = result.getMetaData();
                    colCount = rsmd.getColumnCount();
                    headers = new String[colCount];
                    for(int t = 1; t <= colCount; t++)
                        headers[t-1] = rsmd.getColumnName(t);
                    while(result.next()){
                        String record[] = new String[colCount];
                        for(int i = 0; i < colCount; i++)
                            record[i] = result.getString(i+1);
                        cache.addElement(record);
                    }
                    //fireTableChanged(null);
                }
            } catch (SQLException ex) {
                System.out.println("Can't create Statement.");
                System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
                ex.printStackTrace();
            }finally {
                if (result != null) {
                    try {
                        result.close();
                        System.out.println("ResultSet close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    result = null;
                }
                
                if (state != null) {
                    try {
                        state.close();
                        System.out.println("Statement close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    state = null;
                }
            }
        }
        
        public ResultSet result;
        
        public ResultSetMetaData rsmd;    public void doDelete(String info) {
            try {
                state = getCon().createStatement();
                System.out.println("create Statement.");
                state.execute(info);
                System.out.println("delete the info.");        } catch (SQLException ex) {
                System.out.println("Can't create Statement.");
                System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
                ex.printStackTrace();
            }finally {
                if (result != null) {
                    try {
                        result.close();
                        System.out.println("ResultSet close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    result = null;
                }
                
                if (state != null) {
                    try {
                        state.close();
                        System.out.println("Statement close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    state = null;
                }
            }
        }    public void doInsert(String info) {
             try {
                state = getCon().createStatement();
                System.out.println("create Statement.");
                state.execute(info);
                System.out.println("insert the info.");        } catch (SQLException ex) {
                System.out.println("Can't create Statement.");
                System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
                ex.printStackTrace();
            }finally {
                if (result != null) {
                    try {
                        result.close();
                        System.out.println("ResultSet close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    result = null;
                }
                
                if (state != null) {
                    try {
                        state.close();
                        System.out.println("Statement close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    state = null;
                }
            }
        }    public void doUpdate(String info) {
            try {
                state = getCon().createStatement();
                System.out.println("create Statement.");
                state.execute(info);
                System.out.println("insert the info.");        } catch (SQLException ex) {
                System.out.println("Can't create Statement.");
                System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
                ex.printStackTrace();
            }finally {
                if (result != null) {
                    try {
                        result.close();
                        System.out.println("ResultSet close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    result = null;
                }
                
                if (state != null) {
                    try {
                        state.close();
                        System.out.println("Statement close.");
                    } catch (SQLException sqlEx) {
                        sqlEx.printStackTrace();
                    }
                    
                    state = null;
                }
            }
        }
        
    }
      

  5.   

    String connect="jdbc:mysql://localhost/fdb_product?user=root";
    String userName="root";
    String password="";
    ------------------------------------------------------------------
    String connect="jdbc:mysql://localhost/fdb_product";
    String userName="root";
    String password="";我第一次回贴的时候发现你重复了user,因此我问你看明白没有(误会了你的意思)
    Connection con = Drivermanager.getConnection(connect,userName,password);
    或者形如我的例子中所采用形式:
    con=DriverManager.getConnection("jdbc:mysql://localhost/MVC?user=poemcode&password=aaaa");
    注:这里MVC是数据库名称,用户名为poemcode,密码为aaaa。此例为我在上学期课程设计中的代码,测试通过。
    在你的回贴中“我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。”,真正的应该是把mysql-connector-java-3.1.12-bin.jar加入进去。