IDEA下java连接Mysql数据库

来源:互联网 发布:java ee基础实用教程 编辑:程序博客网 时间:2024/05/22 06:30

1.    没有添加jar程序驱动包导致出现以下问题,即无法找到驱动类。

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

去mysql官网   http://www.mysql.com/downloads/connector/j   手动下载Java连接MySQL的最新驱动包,然后解压放在某文件夹,在系统变量里面的class_path后添加解压文件夹里面的mysql-java-connector.jar的路径,记得在其前面加上英语格式的分号与前面的区分,再通过intellij idea导入即可。

点击 File ->  Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Project Structure界面左侧的“Modules”;

在 “Dependencies” 标签界面下,点击右边绿色的 “+”号,选择第一个选项“JARs or directories...”,选择相应的jar包,点“OK”,jar包添加成功。其他类似错误也可以通过导报解决。     具体可以参考                   http://jingyan.baidu.com/article/fec7a1e5f79e2b1191b4e74f.html  

2.        static final String DB_URL = "jdbc:mysql://localhost:3306/你要用的数据库名称";   
       static final String USER = "root";
           static final String PASS = "root123";

程序中对应的数据库名称改为自己的数据库名称;用户和密码也改成自己的数据库相对应的,否则会报错,端口也要对啊。

3.下面是一个相关的小程序

package Homework;import java.sql.*;import java.util.Scanner;public class Lyq15 {    public static void main(String[] args) {        //声明Connection对象        Connection con;        //驱动程序名        String driver = "com.mysql.jdbc.Driver";        //URL指向要访问的数据库名login        String url = "jdbc:mysql://localhost:3306/login";        //MySQL配置时的用户名        String user = "root";        //MySQL配置时的密码        String password = "331800abcde";        //遍历查询结果集        try        {            //加载驱动程序            Class.forName(driver);            //1.getConnection()方法,连接MySQL数据库!!            con = DriverManager.getConnection(url,user,password);            if(!con.isClosed())                System.out.println("Succeeded connecting to the Database!");            //2.创建statement类对象,用来执行SQL语句!!            Statement statement = con.createStatement();            //要执行的SQL语句            String sql = "select * from login_messag";    //从建立的login数据库的login——message表单读取数据            //3.ResultSet类,用来存放获取的结果集!!            ResultSet rs = statement.executeQuery(sql);            /*System.out.println("-----------------");            System.out.println("执行结果如下所示:");            System.out.println("-----------------");            System.out.println(" 姓名" + "\t" + " 密码");            System.out.println("-----------------");*/            String name = null;            String login_password = null;            while(rs.next())            {                //获取stuname这列数据                name = rs.getString("name");                //获取stuid这列数据                login_password = rs.getString("password");                //首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。                //然后使用GB2312字符集解码指定的字节数组。                name = new String(name.getBytes("ISO-8859-1"),"gb2312");                //输出结果               // System.out.println(name + "\t" + login_password);                //密码输入加判断                System.out.print("What is the password?");                Scanner in = new Scanner(System.in);                String input = in.nextLine();                if(input==login_password){                    System.out.print("Welcome!");                }                else {                    System.out.print("I don't know you.\n");                }            }            rs.close();            con.close();        }        catch(ClassNotFoundException e)        {            //数据库驱动类异常处理            System.out.println("Sorry,can`t find the Driver!");            e.printStackTrace();        }        catch(SQLException e)        {            //数据库连接失败异常处理            e.printStackTrace();        }        catch (Exception e)        {            // TODO: handle exception            e.printStackTrace();        }        finally        {            System.out.println("数据库数据成功获取!!");        }    }}

原创粉丝点击