服务器搭建:servlet+dao+javabean+jsp实现简单的用户登录(二)
军临城下
回复
qq_41068811:
抱歉,上次看到时在上班,之后忘记回复了。
```
package xue.database.dao;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
public class DataSource {
private static String url = ;jdbc:mysql://localhost:3306/learn;; //连接路径
private static String user = ;root;; //用户名
private static String password = ;zxj341204;; //密码
/**
* 打开并返回数据库连接
*/
public static Connection getConnection() {
try {
//创建Mysql数据源对象
MysqlDataSource dataSource = new MysqlDataSource();
//设置连接属性
dataSource.setUrl(url);
dataSource.setUser(user);
dataSource.setPassword(password);
//打开并返回连接
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
```
|