zoukankan      html  css  js  c++  java
  • eclipse 连接 mysql

    1.下载驱动。

    2.eclipse->add extend jars -> 添加驱动。

    3.测试:

    在mysql 建立数据库和表,在eclipse 里对数据库进行操作。

    代码:

    mysql:

    create database test;
    
    create table user (name, varchar(20), password varchar(20));
    
    insert into user values ('xiaotao', '123456');
    

    Java (查看和添加记录):

    package sql;
    import java.lang.reflect.Executable;
    import java.sql.*;
    
    public class Connector {
    	public static void main(String[] args) {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			System.out.println("Success loading Mysql Driver");
    		}catch (Exception e) {
    			System.out.println("Error loading Mysql Driver!");
    			e.printStackTrace();
    		}
    		try {
    			Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
    			System.out.println("Success connect Mysql server!");
    			Statement stmt = connect.createStatement();
    			ResultSet rs = stmt.executeQuery("select *from user");
    			while(rs.next()) {
    				System.out.println(rs.getString("name"));
    			}
    			
    			PreparedStatement stmt2 = connect.prepareStatement("insert into user value(?, ?)");
    			stmt2.setString(1, "sunhongtao");
    			stmt2.setString(2, "123");
    			stmt2.executeUpdate();
    		}
    		catch (Exception e) {
    			System.out.println("get data error!");
    			e.printStackTrace();
    		}
    	}
    }
    

     ======================================================

    补充相关的小常识:

    1、Class.forName("com.jdbc.mysql.Driver"); // 显示的加载驱动

    String url = "jdbc:mysql://localhost:3306/test"; // 要连接的数据库url 其中test表示数据库名字

    String username = "root"; // 数据库管理者用户名

    String password = "123456"; // 密码

    Connection conn = DriveManager.getConnection("url", "username", "password"); // 获取相应数据库连接

    2、preparedStatement和Statement的区别:

    大概,前者可以完全代替后者,就是会多出几条语句。

    String updateString = "UPDATE COFFEES SET SALES = 75 " + "WHERECOF_NAME LIKE ′Colombian′";  
    Statement stmt = conn.createStatement(); stmt.executeUpdate(updateString);

    和:

    PreparedStatement updateSales = con.prepareStatement("UPDATE COFFEES SETSALES = ? WHERE COF_NAME LIKE ? ");  
    updateSales.setInt(1, 75);  
    updateSales.setString(2, "Colombian");  
    updateSales.executeUpdate();  
    

    3、preparedStatement和Statement有三个执行sql语句的方法:

    execute、executeQuery和executeUpdate.

    区别:

    方法executeQuery
            用于产生单个结果集的语句,例如 SELECT 语句。 被使用最多的执行 SQL 语句的方法是 executeQuery。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句。

            方法executeUpdate
            用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。

            使用executeUpdate方法是因为在 createTableCoffees 中的 SQL 语句是 DDL (数据定义语言)语句。创建表,改变表,删除表都是 DDL 语句的例子,要用 executeUpdate 方法来执行。你也可以从它的名字里看出,方法 executeUpdate 也被用于执行更新表 SQL 语句。实际上,相对于创建表来说,executeUpdate 用于更新表的时间更多,因为表只需要创建一次,但经常被更新。


            方法execute:
            用于执行返回多个结果集、多个更新计数或二者组合的语句。因为多数程序员不会需要该高级功能

            execute方法应该仅在语句能返回多个ResultSet对象、多个更新计数或ResultSet对象与更新计数的组合时使用。当执行某个已存储过程或动态执行未知 SQL 字符串(即应用程序程序员在编译时未知)时,有可能出现多个结果的情况,尽管这种情况很少见。

    参考自:http://blog.csdn.net/wade1000/article/details/26219161

    4、ResultSet是返回结果的容器可以由preparedStatement和Statement的getResultSet()方法获得。

  • 相关阅读:
    VS2010 自动跳过代码现象
    Reverse Linked List II 【纠结逆序!!!】
    Intersection of Two Linked Lists
    Linked List Cycle II
    Remove Nth Node From End of List 【另一个技巧,指针的指针】
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Remove Duplicates from Sorted List
    Linked List Cycle
    Dungeon Game
  • 原文地址:https://www.cnblogs.com/icode-girl/p/5557595.html
Copyright © 2011-2022 走看看