eclipsejee怎么连接mysql数据库

如题所述

首先你得有一个jdbc的架包

然后

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
 
public class Util {
    public static Connection getConnection(){
        Connection con = null;
        String user = "root";//数据库登陆用户名
        String password = "root";//数据库登陆密码
        String url = "jdbc:mysql://localhost:3306/password";//数据库库名
        String driver = "com.mysql.jdbc.Driver";
        try{
            Class.forName(driver);
            con = DriverManager.getConnection(url,user,password);
        }catch(Exception e){
             
        }
        return con;
         
    }
    public static void main(String[] args) {
        Connection con = getConnection();
        String sql = "insert into stu values(null,\'hello\')";
        PreparedStatement p = null;
        try{
            p = con.prepareStatement(sql);
            p.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try{
                p.close();
                con.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}

温馨提示:答案为网友推荐,仅供参考