eclipse 更新mysql_eclipse 连接 mysql

本文介绍了如何在Eclipse中更新MySQL连接,包括下载驱动、添加到项目中,并通过Java代码进行数据库操作。同时,解释了Class.forName加载驱动、Statement与PreparedStatement的区别以及执行SQL语句的方法,还提到了ResultSet作为查询结果的容器。
摘要由CSDN通过智能技术生成

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 字符串(即应用程序程序员在编译时未知)时,有可能出现多个结果的情况,尽管这种情况很少见。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值