zoukankan      html  css  js  c++  java
  • Eclipse 连接MySql数据库总结

    Eclipse 连接MySql数据库总结

    一、在MySql中创建数据库,并创建表,向表中插入数据

    1、创建数据库

    create database select_test

    2、创建表

    create table teacher_table(
        Id int,
        Name Varchar(20),
        Sex Varchar(2)
         
    )

    3、向表中插入数据(这里插入三条测试数据)

    insert into teacher_table values(1,'zhangsan','ma');
    insert into teacher_table values(2,'lisi','fe');
    insert into teacher_table values(3,'wangwu','ma');

      

    二、配置Eclipse。

    配置之前请先下载mysql-connector-java-5.1.15-bin.jar文件。

    右键单击包所在的工程包(project),Build Path ---> Configure Build Path,在弹出的窗口中选择 Add External JARs。把你下载并解压出来的mysql-connector-java-5.1.15-bin.jar选中。如图

     三、编写连接代码。

    数据库名:select_test

    用户名:root

    密码:123456

    连接成功后显示teacher_table表中的数据。

    import java.sql.*;
     
    class ConnMySql {
     
        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            Class.forName("com.mysql.jdbc.Driver");
             
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/select_test",
                    "root","123456");
            Statement stmt =  conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from teacher_table");
             
            while (rs.next()) {
                System.out.println(rs.getInt(1) + " "
                        +rs.getString(2) + " "
                        +rs.getString(3) );
                }
             
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();  
            }
            if (conn != null) {
                conn.close();  
            }
        }
     
    }

     总结:Elipse连接MySql数据库要求下载mysql-connector-java-5.1.15-bin.jar文件。 

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    UEditor使用报错Cannot set property 'innerHTML' of undefined
    freemarker如何在url中传递中文参数
    freemarker字符串转换成日期和时间
    freemarker 类型转换
    内存分析工具 MAT 的使用
    Ubuntu13.04下Eclipse中文乱码解决
    自定义上下文对话框
    格局中@null的代码实现方式
    Android xml资源文件中@、@android:type、@*、?、@+含义和区别
    探讨:你真的会用Android的Dialog吗?
  • 原文地址:https://www.cnblogs.com/liyingxiang/p/5861608.html
Copyright © 2011-2022 走看看