解决方案 »

  1.   

    连接数据库语句对了吗,jar包引了吗
      

  2.   

    有没有相应的驱动包!
    一般是如下格式(mysql):
    Class.forName("com.mysql.jdbc.Driver);
    DriverManager.getConnection(url,user,pwd);
      

  3.   

    直接拿jsp连数据库? 略叼啊
      

  4.   

    这个,要在后台连接数据库,在JPS页面显示
      

  5.   

    哎,看在你是新手的份上,我强烈建议你去百度或是谷歌,这样JDBC连接数据库的代码一大堆。
      

  6.   


    public class YamlConfig {  private final static Log log = LogFactory.getLog(YamlConfig.class);
     
    public static class ConfigEntry{

    public String jdbc_url;
    public String jdbc_user;
    public String jdbc_password;

    public String admin_user;
    public String admin_password;

    public String domain_url;

    }

    private static YamlConfig  instance = new YamlConfig();

    private ConfigEntry configEntry;

    private String webappRoot;

    private String contextPath = "/";

    private YamlConfig(){
    webappRoot = System.getProperty("quotation.root");
    try{
    configEntry= Yaml.loadType(new File(webappRoot,"WEB-INF/config.yml"),ConfigEntry.class);

    } catch (Exception e){
    log.error("Load config.yml error.",e);
    }
    if(configEntry == null || configEntry.domain_url == null){
    log.error(" Can not find domain_url in the config.yml.");

    throw new RuntimeException("Error in the config.yml");
    }
    if(configEntry.domain_url != null && configEntry.domain_url.startsWith("http")){
    configEntry.domain_url = "http://"+configEntry.domain_url;
    }
    if(configEntry.domain_url.endsWith("/")){
    configEntry.domain_url= configEntry.domain_url.substring(0,configEntry.domain_url.length()-1);
    }
    }

    public static YamlConfig getInstance(){
    return instance;

    }
    public ConfigEntry getConfigEntry(){
     return configEntry;
     
    }
    public String GetWebappRoot(){
    return webappRoot;

    }

    public void setContextPath(String path){
     contextPath= path;
     
    }

    public String getDomain_Url(){
    return configEntry.domain_url;
    }


    public String getContextUrl(){
     return configEntry.domain_url+ contextPath + "/";
     
     
    } public String getWebappRoot() {
    return webappRoot;
    }

    }先配置,第一个文件,文件名YamlConfig.java
      

  7.   

    第二个文件:获取数据源。public class DataSourceFactoryBean implements FactoryBean<BasicDataSource>, DisposableBean { private static final int DBCP_INITIAL_SIZE = 5;
    private static final int DBCP_MAX_ACTIVE = 30;
    private static final int DBCP_MIN_IDLE = 5;
    private static final int DBCP_MAX_IDLE = 10;
    private static final int DBCP_MAX_WAIT = 20000;
    private static final int DBCP_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 300000;
    private static final int DBCP_MIN_EVICTABLE_IDLE_TIME_MILLIS = 320000;
    private static final int DBCP_REMOVE_ABANDONED_TIMEOUT = 150; //private static Logger log = LoggerFactory.getLogger(BasicDataSourceFactoryBean.class); private BasicDataSource dataSource; @Override
    public BasicDataSource getObject() throws Exception {
    final String driver = "com.mysql.jdbc.Driver";

    // if (log.isDebugEnabled()) {
    // log.debug("jdbc init, url : {}, username : {}, password : {}", new String[] { url, username, password });
    // }
    ConfigEntry entry = YamlConfig.getInstance().getConfigEntry();
    dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(entry.jdbc_url);
    dataSource.setUsername(entry.jdbc_user);
    dataSource.setPassword(entry.jdbc_password); dataSource.setInitialSize(DBCP_INITIAL_SIZE);
    dataSource.setMaxActive(DBCP_MAX_ACTIVE);
    dataSource.setMinIdle(DBCP_MIN_IDLE);
    dataSource.setMaxIdle(DBCP_MAX_IDLE);
    dataSource.setMaxWait(DBCP_MAX_WAIT);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setValidationQuery("select 1");
    dataSource.setTestOnBorrow(true);
    dataSource.setTestWhileIdle(true);
    dataSource.setTimeBetweenEvictionRunsMillis(DBCP_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
    dataSource.setMinEvictableIdleTimeMillis(DBCP_MIN_EVICTABLE_IDLE_TIME_MILLIS);
    dataSource.setRemoveAbandoned(true);
    dataSource.setRemoveAbandonedTimeout(DBCP_REMOVE_ABANDONED_TIMEOUT);
    dataSource.setLogAbandoned(true); return dataSource;
    } @Override
    public Class<BasicDataSource> getObjectType() {
    return BasicDataSource.class;
    } @Override
    public boolean isSingleton() {
    return true;
    } @Override
    public void destroy() throws Exception {
    if (dataSource != null) {
    dataSource.close();
    }
    }}文件名称:DataSourceFactoryBean.java
      

  8.   

    连接你的数据库
    jdbc_url: jdbc:mysql://127.0.0.1:3306/quotation
    jdbc_user: root  
    jdbc_password: root  admin_user: shuodao
    admin_password: shuodao2014@@domain_url: http://127.0.0.1:8080
    此文件名:config.yml。此时数据库就可以操作了。
      

  9.   

      这个mysql-connector-java-5.0.5-bin.jar   有木有?没有 你肯定搞不定,没法调用mysql  Connection conn = null;   //置空
        Statement stat = null; 
        ResultSet rs = null;    Class.forName("com.mysql.jdbc.Driver");   //jar包
        String url = "jdbc:mysql://localhost:3306/jsp";   //jsp是数据库名称
        String user = "root";   
        String password = "123456";   //密码
        conn = DriverManager.getConnection(url,user,password); 
        stat = conn.createStatement(); 
        rs = stat.executeQuery("select * from student");