- 论坛徽章:
- 0
|
- package db;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
-
- /**
- * 和数据库连接
- * @author HXM
- *
- */
- public class DbConnction {
- String driverName,url,userName,passwd;
- public DbConnction(){
- driverName="com.mysql.jdbc.Driver";
- url="jdbc:mysql://localhost:3306/mybookshop";
- userName="root";
- passwd="123456";
- //1.加载驱动
- try {
- Class.forName(driverName);
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //2.得到和数据库的连接
- public Connection getConnection(){
- try {
- return DriverManager.getConnection(url,userName,passwd);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public static void main(String[] args){
- DbConnction s=new DbConnction();
- if(s.getConnection()==null){
- System.out.println("失败");
- }else{
-
- System.out.println("成功");
- }
-
- }
- }
复制代码 |
|