【问题标题】:Connect web java to database system using MySQL使用 MySQL 将 web java 连接到数据库系统
【发布时间】:2015-01-08 19:55:15
【问题描述】:

我创建了一个带有登录信息帐户(用户名、密码)的 java 网站。现在我想使用 MySQL ver5.6 连接到数据库。我已经有了 ID、用户名、密码的数据库。

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public abstract class DBConnectionDAO {

/** Connection */
protected Connection connection;

/** className */
private String className = "com.mysql.jdbc.Driver";

/** stringConnection */
private String stringConnection = "jdbc:mysql://localhost/test?user=root&password=abc123&characterEncoding=UTF-8";

/**
 * @Constructor Phuong thuc khoi tao
 */
public DBConnectionDAO() {
    try {
        Class.forName(className);
        // Open connection
        connection = DriverManager.getConnection(stringConnection);
    } catch (ClassNotFoundException e) {
        System.err.println("Class not found! Please review your library");
    } catch (SQLException e) {
        e.printStackTrace();
        System.err.println("Loi truy van");
    }
}

}

【问题讨论】:

    标签: java mysql database logging web


    【解决方案1】:

    公共类数据库{

    Connection conn = null;
    int id;
    
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver Loaded Successfully..");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            }
        }
    
    public Connection connect(){
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename", "root", "root");
            if(conn!=null)
            {
                System.out.println("Database Connected Successfully...");
            }
    
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    
    }
    

    【讨论】:

    • 感谢 user1722880,但是当我编辑类 dao 时,类访问用户 DAO 有问题。这是我的代码
    猜你喜欢
    • 2022-01-01
    • 2015-02-06
    相关资源
    最近更新 更多