先加驱动包如图所示
后边使用的导入包都是java.sql的如果不是说明有错误的
增加功能
package Train2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Create {
public static void main(String[] args) {
try {
//通过反射加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
(
//通过驱动获得连接
Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/Hero?characterEncoding=UTF-8",
"root",
"123");
//获得statement对象
Statement s = c.createStatement();
)
{
for (int i = 0; i < 100; i++) {
//创建sql语句
String sql = "insert into user value("+(i+1)+",'"+"英雄"+i+"')";//创建一个user表
//statement对象执行sql语句
s.execute(sql);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
删除功能
package Train2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
(
Connection c = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/Hero?characterEncoding=UTF-8",
"root",
"123");
Statement s = c.createStatement();
)
{
String sql = "delete from user where id = 99";
//返回值是true说明此句使用查询语句的,有返回值可以用result结果集接受
//如果是false说明就是执行语句不用结果集接受
boolean flag = s.execute(sql);
System.out.println(flag);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
更新功能
package Train2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Updatae {
public static void main(String[] args) {
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
(
Connection c = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/Hero?characterEncoding=UTF-8",
"root",
"123");
Statement s = c.createStatement();
)
{
String sql = "update user set name = '王八蛋' where id = 10";
boolean flag = s.execute(sql);
System.out.println(flag);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
查询功能
package Train2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Query {
public static void main(String[] args) {
try {
//利用反射原理将驱动初始化
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
( //通过驱动得到连接
Connection c = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/hero?characterEncoding=UTF-8",
"root",
"123");
//由连接得到statement对象statement英文有翻译、执行、陈述声明的意思
Statement s = c.createStatement();
)
{
//编写sql语句
String sql = "select * from user";
//statement对象执行sql语句得到结果集
ResultSet rs = s.executeQuery(sql);
//遍历结果集方式一:按照字段顺序123...获得结果 rs.next()判断下边是否有值了
// while(rs.next()) {
// int i = rs.getInt(1);
// String name = rs.getString(2);
// System.out.printf("%d : %s%n",i,name);
// }
//遍历结果集方式二:按照字段名字.获得结果 rs.next()判断下边是否有值了
//两种方式在后边有对照图
while(rs.next()) {
int i = rs.getInt("id");
String name = rs.getString("name");
System.out.printf("%d : %s%n",i,name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
查询的两种方式对照图