eclipse 如何连接数据库(mysql)

本文前两天突发奇想把自己的所有的账号都放到数据库中,因为平时喜欢逛论坛,可是发现自己还不会使用数据库,于是乎想到用office自带的ACCSE搞定,瞬间就打开了右键新建了一个ACCSE,然后各种建表的过程。。。。。

写好之后,发现电脑中找不到数据源连接的按钮,于是上网百度,发现很多win7系统自动把这个选项隐藏了

下面的动作将其调出来,电脑下方菜单栏中中鼠标右键属性,然后选择开始菜单---》自定义----》系统管理工具选择显示,ok

在开始菜单中找到这个选项之后,点击进入,找到数据源ODBC选项,然后选择添加,发现根本找不到ACCSE数据源,瞬间崩溃。。。。。。。。。。。。。。

从网上找到攻略,发现世间还是有爱的

大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑

方法就是找到C:\Windows\SysWOW64\odbcad32.exe,双击进去就和正常的完全一样了,然后很高兴的这么干了,代码也欢天喜地的写了,代码如下:

   


import java.sql.*;  
    class s{  
        public static void main(String args[]){  
           // String driver = "com.mysql.jdbc.Driver";
            String url="jdbc:mysql://127.0.0.1:3306/zxc";  
            String user="root";  
            String password="zxcv";  
            String ls_1="select * from second_sql where 编号=1";//"select firstname from employee";  
            try{  
                //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动程序              ,其实JDBC驱动程序有四种  
            	//Class.forName(driver);
                Connection con=DriverManager.getConnection(url, user, password);//建立连接  
                Statement stmt=con.createStatement();//执行SQL  
                ResultSet result=stmt.executeQuery(ls_1);  
      
                System.out.print("编号    ");  
                System.out.print("password  ");  
                System.out.print("lastname  ");  
                System.out.print("firstname ");  
                System.out.print("age   ");  
                System.out.print("address   ");  
                System.out.print("city  ");  
                while(result.next()){  
                	System.out.println();
                    System.out.print(result.getInt(1)+" |   ");  
                    System.out.print(result.getString(2)+"  |   ");  
                    System.out.print(result.getString(3)+"  |   ");  
                    System.out.print(result.getString(4)+"  |   ");  
                    System.out.print(result.getString(5)+"  |   ");  
                    System.out.print(result.getString(6)+"  |   ");  
                    System.out.println(result.getString(7)+"    |   ");  
                    System.out.println();
                }  
                result.close();  
                stmt.close();  
                con.close();  
            }catch(SQLException sqle){  
                System.out.println(1+sqle.toString());  
            }  
            catch(Exception e){  
                System.out.println(2+e.toString());  
            }  
        }  
    }  

点击运行,瞬间我就哭哭哭哭哭哭哭哭哭哭哭哭哭

何解??????????、

出现的错误是DSN的类型错误,从网上baidu之后,发现很多人和我的错误都一样,仔细看了之后说的是,我的jdk和系统是64位的,用的数据库却是32位的,为了证明这句话是错误的我还特意看了一下自己的ACCSEE也就是office是多少位的。。。。。

看的方法网上很多,突然间发现别人说的真对,我的就是32位的

没有办法了,要么重装系统???

我去。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

必须不行啊,果断重新装一个数据库,从网上下载了一个mysql,我的版本是mysql-installer-community-5.7.3.0-m13.2063434697,装了之后发现太复杂了,不会用,最后在我们实验室的大神指导下,慢慢的搞定了,程序也随之稍微做了调整,

代码如下:

   


import java.sql.*;  
    class s{  
        public static void main(String args[]){  
        	String driver = "com.mysql.jdbc.Driver";
            String url="jdbc:mysql://127.0.0.1:3306/zxc";  
            String user="root";  
            String password="zxcv";  
            String ls_1="select * from second_sql where 编号=1";//"select firstname from employee";  
            try{  
                //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动程序              ,其实JDBC驱动程序有四种  
            	Class.forName(driver);
                Connection con=DriverManager.getConnection(url, user, password);//建立连接  
                Statement stmt=con.createStatement();//执行SQL  
                ResultSet result=stmt.executeQuery(ls_1);  
      
                System.out.print("编号    ");  
                System.out.print("password  ");  
                System.out.print("lastname  ");  
                System.out.print("firstname ");  
                System.out.print("age   ");  
                System.out.print("address   ");  
                System.out.print("city  ");  
                while(result.next()){  
                	System.out.println();
                    System.out.print(result.getInt(1)+" |   ");  
                    System.out.print(result.getString(2)+"  |   ");  
                    System.out.print(result.getString(3)+"  |   ");  
                    System.out.print(result.getString(4)+"  |   ");  
                    System.out.print(result.getString(5)+"  |   ");  
                    System.out.print(result.getString(6)+"  |   ");  
                    System.out.println(result.getString(7)+"    |   ");  
                    System.out.println();
                }  
                result.close();  
                stmt.close();  
                con.close();  
            }catch(SQLException sqle){  
                System.out.println(1+sqle.toString());  
            }  
            catch(Exception e){  
                System.out.println(2+e.toString());  
            }  
        }  
    }  

当然没有把用我的建的账号密码的那张表做实验了,随便添加的一张表!!!!!

到了mysql的使用稍微需要说的一点是,需要安装一个连接器,我安装的是mysql-connector-java-5.0.5,这个解压之后,我放在了D盘中,把其中的jar问价的路径添加到系统中的classpath下,具体添加的方法很简单,就不做脑残介绍了

使用eclipse还需要添加外部引用包,右击工程文件,选择properties----》java Build Path------》Libraries------》Add External JARs ,剩下的就是等着喝茶庆功酒OK了

发布了105 篇原创文章 · 获赞 10 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章