Mysql、SQL Developer、maven、java的使用

来源:互联网 发布:查淘宝宝贝隐形降权 编辑:程序博客网 时间:2024/05/23 16:55
  1. Mysql的安装和使用:
    http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#c5
  2. SQL Developer连接Mysql数据库:
    http://www.cnblogs.com/wangqianqiannb/p/6039554.html
  3. maven中添加Mysql的依赖:
   <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.25</version>  </dependency>

4 java连接数据库

import java.sql.*;public class MysqlTest {    public static void main(String[] args){        // 驱动程序名        String driver = "com.mysql.jdbc.Driver";        // URL指向要访问的数据库名students        String url = "jdbc:mysql://192.168.8.138/database1";        // MySQL配置时的用户名        String user = "root";        // MySQL配置时的密码        String password = "1234";        String id;        try {            // 加载驱动程序            Class.forName(driver);            // 连接数据库            Connection conn = DriverManager.getConnection(url, user, password);            if(!conn.isClosed())                System.out.println("Succeeded connecting to the Database!");            // statement用来执行SQL语句            Statement statement = conn.createStatement();            // 要执行的SQL语句            String sql = "select * from students";            // 结果集            ResultSet rs = statement.executeQuery(sql);            while(rs.next())  {                // 选择id这列数据                id = rs.getString("id");                // 输出结果                System.out.println(rs.getString("id") + "\t" + id);            }            rs.close();       conn.close();  }        catch(ClassNotFoundException e) {            System.out.println("Sorry,can`t find the Driver!");            e.printStackTrace();        } catch(SQLException e) {            e.printStackTrace();        } catch(Exception e) {            e.printStackTrace();        }    }}
0 0