Dmeo1
public class Test1 {
public static void main(String[] args){
//1.加载数据驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//建立连接,获得Connection对象
Connection conn=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day01","root","root");
System.out.println("連接成功");
} catch (SQLException e) {
e.printStackTrace();
}
//关闭资源
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Demo2
import java.sql.DriverManager;
import java.sql.Statement;
/**先见表
CREATE TABLE `master` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` varchar(12) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`money` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
//需求:使用Statement对象往dog表添加一条记录
//staticment Statement 是 Java 执行数据库操作的一个重要接口,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。Statement对象,用于执行不带参数的简单SQL语句。
//结果集(ResultSet)是数据中查询结果返回的一种对象,可以说结果集是一个存储查询结果的对象,但是结果集并不仅仅具有存储的功能,他同时还具有操纵数据的功能,可能完成对数据的更新等.
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
String url="jdbc:mysql://127.0.0.1:3306/day01";
String user="root";
String password="root";
String sql="INSERT INTO dog(name,health,love,strain) VALUES ('aaa',90,100, 'bbb')";//执行insert操作的语句
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
}
//2.获得连接对象
try {
conn=DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
} catch (Exception e) {
}
//3.获取Statement对象
try {
stmt=conn.createStatement();
//stmt.execute(sql);
stmt.executeUpdate(sql);
} catch (Exception e) {
}
//关闭资源
try {
stmt.close();
conn.close();
} catch (Exception e) {
}
Demo3
//需求:使用ResultSet查询dog信息
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/day01";
String user = "root";
String password = "root";
String sql = "SELECT * FROM dog";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
// 遍历rs
// rs.next();指针下移
// while (rs.next()) {
// // 在MySql数据库中index从1开始
// System.out.println(rs.getInt(1) + "\t"); 1是指表格的第一列
// System.out.println(rs.getString(2) + "\t");
// System.out.println(rs.getInt("health") + "\t");用字符串来指明是那列
// System.out.println(rs.getInt("love") + "\t");
// System.out.println(rs.getString("strain") + "\t");
//
// System.out.println("===============================");
// }
//可以直接用Objiect来指明队列的数据类型
while (rs.next()) {
// 在MySql数据库中index从1开始
System.out.println(rs.getObject(1) + "\t");
System.out.println(rs.getObject(2) + "\t");
System.out.println(rs.getObject("health") + "\t");
System.out.println(rs.getObject("love") + "\t");
System.out.println(rs.getObject("strain") + "\t");
System.out.println("===============================");
}
//if语句声明不为空的时候才关闭资源
} catch (Exception e) {
} finally {
try {
if (null != rs) {//这个结果集合不为空
rs.close();
}
if (null != stmt) {//操作mysqal对象不为空
stmt.close();
}
if (null != conn) {//
conn.close();
}
} catch (Exception e2) {
}
}
Demo4
更新dog表
Connection conn = null;
Statement stmt = null;
String url = "jdbc:mysql://localhost:3306/day01";
String user = "root";
String password = "root";
String sql = "UPDATE dog SET health=86,love=11 WHERE id=1";
// 1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
}
// 2.获得连接对象
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
} catch (Exception e) {
}
// 3.获取Statement对象
try {
stmt = conn.createStatement();
// stmt.execute(sql);
stmt.executeUpdate(sql);
} catch (Exception e) {
}
Demo5
Connection conn = null;
Statement stmt = null;
String url = "jdbc:mysql://localhost:3306/day01";
String user = "root";
String password = "root";
String sql = "DELETE FROM dog WHERE id=1";
// 1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
}
// 2.获得连接对象
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
} catch (Exception e) {
}
// 3.获取Statement对象
try {
stmt = conn.createStatement();
// stmt.execute(sql);
stmt.executeUpdate(sql);
} catch (Exception e) {
}
try {
stmt.close();
conn.close();
} catch (Exception e) {
}
Demo6
//将写好的增删改查代码运行
public class Test6 {
/**
* 查询所以的主人信息
*/
public void findAll(){
}
/**
* insert一条记录
*/
public void insert(){
}
/**
* 更新一条记录
*/
public void update(){
}
/**
* 删除一条记录
*/
public void delete(){
}
}