你引的jar包路径有中文 。把那个直连包直接扔到项目的lib文件夹下,

解决方案 »

  1.   

    右键 - >  Build Pah  - > add External Archives,添加进去;
      

  2.   


    我在这个工程的WEB-INF中的lib文件有放sqljdbc4.jar
      

  3.   

    最上面的那位。 我有点不明白你的意思。 我的第一段代码和第二段代码都是在一个JSP中的。test1和test2.。下面的代码都一样, 只是这边的连接数据的代码不一样,然后就说是无法驱动,
      

  4.   

    Class.forName()   有没有写?
      

  5.   

    直接看的话就是因为找不到合适的驱动,
    1、路径去掉中文
    2、项目上邮件,buildPath配置一下,在library里面选add jars ,选择你工程lib目录下的 jar包。
    3、jar包最好copy到lib目录里面而不要link过去。否则项目换到其他环境下或者你引用的jar包换位置了会出错。
      

  6.   

    也可能是这个原因
    这个Class。forName写哪里的啊?
      

  7.   

    你这个数据库驱动都没有注册,当然链接不上了。至于你第一个为什么能成功,可能是eclipse内置的数据库吧,已经注册成功了
      

  8.   

    Class.forName()表示加载驱动,写在连接前面
    1.导包 2.注册驱动 强行把一个类加载到内存 导致静态运行
           Class.forName("oracle.jdbc.driver.OracleDriver");
    3.连接数据库
       
    4.建立一个语句对象,进行操作
                 
    5.把语句发到Oracle
                 
    5.2 把语句发到oracle 
                 
      

  9.   

    也可能是这个原因
    这个Class。forName写哪里的啊?import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;public class Connect2DB {
    // DataBase Name
    private static String DBNAME = "mysqldb";
    // DataBase UserName
    private static String DBUSER = "root";
    // DataBase UserPassWord
    private static String DBPASSWORD = "sa";
    // Table Name
    private static String tableName = "user";
    // Driver
    private static String DRIVER = "com.mysql.jdbc.Driver";
    // Driver URL
    private static String URL = "jdbc:mysql://localhost:3306/" + DBNAME;
    //connection
    private static Connection conn = null;

    private Connect2DB(){}
    /**
     * 获取与数据库的链接。单例模式
     * @return Connection
     */
    public static Connection getConnection() {
    if (conn == null) {
    try {
    // 加载MySQL驱动
    Class.forName(DRIVER);
    // 连接数据库
    conn = DriverManager.getConnection(URL, DBUSER, DBPASSWORD);
    } catch (ClassNotFoundException e) {
    throw new RuntimeException("驱动的类没找到。");
    } catch (SQLException e) {
    throw new RuntimeException("获取连接失败!");
    }
    }
    System.out.println("建立连接成功!");
    return conn;
    }

    /**
     * 关闭与数据库的链接
     * @return boolean
     */
    public static void close(Connection conn){
    if (conn != null) {
    try {
    conn.close();
    } catch (SQLException e) {
    throw new RuntimeException("建立与数据库的连接失败!");
    }
    }
    System.out.println("关闭连接成功!");
    }

    /**
     * 插入记录
     * @return
     */
    public static void insert(List<User> list) {
    int count = 0;
    Connection conn = Connect2DB.getConnection();
    for (User user : list) {
    String username = user.getName();
    String userpwd = user.getPassword();
    String userid = user.getUserID();
    String sql = "INSERT INTO " + tableName
    + "(user_id,name,password) VALUES ('"+userid+"' , '" + username + "' , '"
    + userpwd + "')";
    Statement st = null;
    try {
    st = conn.createStatement();
    System.out.println(sql);
    count += st.executeUpdate(sql);
    } catch (SQLException e) {
    throw new RuntimeException("数据插入失败!");
    } finally {
    try {
    st.close();
    } catch (SQLException e) {
    new RuntimeException("Statement 关闭失败!");
    }
    }
    }
    System.out.println("数据插入成功!");
    System.out.println("共插入 " + count + " 行数据");
    }

    /**
     * 更新记录
     */
    public static void update(User user){
    Connection conn = Connect2DB.getConnection();
    String username = user.getName();
    String userid = user.getUserID();
    int count = 0;
    String sql = "update user set name='"+username+"' where user_id='"+userid+"'";
    Statement st =null;
    try {
    st = conn.createStatement();
    count += st.executeUpdate(sql);
    } catch (SQLException e) {
    throw new RuntimeException("更新失败!");
    } finally {
    try {
    st.close();
    } catch (SQLException e) {
    new RuntimeException("Statement 关闭失败!");
    }
    }
    System.out.println("更新成功!");
    System.out.println("共更新 " + count + " 行数据");
    }

    /**
     * 查询记录
     */
    public static void query(Map<String, String> map){
    Connection conn = Connect2DB.getConnection();
    //设置sql语句,并进行条件拼接
    StringBuffer sql = new StringBuffer("select * from " + tableName  +" where 1=1");
    if (!map.isEmpty()) {
    Set<Map.Entry<String, String>> s =map.entrySet();
    for (Entry<String, String> entry : s) {
    sql.append(" and ");
    sql.append(entry.getKey()+"='"+entry.getValue()+"'");
    }
    System.out.println(sql);
    }
    Statement st = null;
    ResultSet rs = null;
    try {
    st = conn.createStatement();
    rs = st.executeQuery(sql.toString());
    if (!rs.next()) {
    System.out.println("没有指定的用户。");
    return;
    }
    System.out.println("id\tname\tpassword");
    while (rs.next()) {
    String id = rs.getString("user_id");
    String name = rs.getString("name");
    String password = rs.getString("password");
    System.out.println(id+"\t"+name+"\t\t"+password);
    }
    } catch (SQLException e) {
    throw new RuntimeException("执行查询失败!");
    } finally {
    try {
    st.close();
    } catch (SQLException e) {
    new RuntimeException("Statement 关闭失败!");
    }
    }
    System.out.println("查询完成!");
    }


    /**
     * 删除记录
     */
    public static void delete(Map<String, String> map){
    Connection conn = Connect2DB.getConnection();
    //设置sql语句,并进行条件拼接
    StringBuffer sql = new StringBuffer( "delete from "+tableName + " where 1=1");
    if (!map.isEmpty()) {
    Set<Map.Entry<String, String>> s =map.entrySet();
    for (Entry<String, String> entry : s) {
    sql.append(" and ");
    sql.append(entry.getKey()+"='"+entry.getValue()+"'");
    }
    System.out.println(sql);
    }

    Statement st = null;
    try {
    st = conn.createStatement();
    int count = st.executeUpdate(sql.toString());
    System.out.println("删除 " +count+ " 条数据。");
    } catch (SQLException e) {
    throw new RuntimeException("删除数据失败!");
    } finally {
    try {
    st.close();
    } catch (SQLException e) {
    new RuntimeException("Statement 关闭失败!");
    }
    }
    }
    }
      

  10.   

    也可能是这个原因
    这个Class。forName写哪里的啊?
    如上,写在第一行