package day17_1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class demo {
/**
* * ClassNotFoundException:
* > 没导驱动包
* >
*
* SQLException:
* > 检查3个参数:url、username、password是否正确
* > 检查是否开启了mysql服务器!
* @throws ClassNotFoundException
* @throws SQLException
*/
//增删改函数
@Test
public void fun1() throws SQLException, ClassNotFoundException{
/*
* jdbc四大配置参数:
* > driverClassName:com.mysql.cj.jdbc.Driver 需记忆
* > url:jdbc:mysql://localhost:3306/tzy_data?useSSL=false&serverTimezone=UTC 需记忆格式tzy_data为数据库名字
* > username:root
* > password:123
*/
Connection conn=null;
Statement stmt=null;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
//Class.forName("com.mysql.cj.jdbc.Driver");// 加载驱动程序,此次无需更改
System.out.println("registered success!");//输出registered success表示驱动加载成功
//创建对象连接,连接数据库,此例中employeedbms为数据库名,root为用户名,123456位密码,该三项均需根据实际情况进行修改,其它代码无需变动。注:确保数据库名、用户名和密码的准确性。
conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/tzy_data?useSSL=false&serverTimezone=UTC","root","280274");
System.out.println("connection success!");//输出connection success表示数据库已成功连接
//通过Connection对象创建statement
//>statement为语句发送器
//调用它的 int executeUpdate(String sql)它可以发送DML、DDL
stmt = conn.createStatement();
//使用Statement发送语句
// String sql="INSERT INTO students VALUES('201600001','唐凯','男',49)";
String sql="UPDATE students SET name='唐凯凯',age='10' WHERE number='201600001'";
// String sql="DELETE FROM students WHERE name='唐凯凯'";
int r=stmt.executeUpdate(sql);
System.out.println("被修改的行数:"+r);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
finally{
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
}
}
//查询函数
@Test
public void fun2() throws ClassNotFoundException, SQLException{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
//Class.forName("com.mysql.cj.jdbc.Driver");// 加载驱动程序,此次无需更改
System.out.println("registered success!");//输出registered success表示驱动加载成功
//创建对象连接,连接数据库,此例中employeedbms为数据库名,root为用户名,123456位密码,该三项均需根据实际情况进行修改,其它代码无需变动。注:确保数据库名、用户名和密码的准确性。
conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/tzy_data?useSSL=false&serverTimezone=UTC","root","280274");
System.out.println("connection success!");//输出connection success表示数据库已成功连接
//通过Connection对象创建statement
stmt = conn.createStatement();
//使用Statement发送语句
String sql="SELECT * FROM students ";
//使用ResultSet接收表
rs=stmt.executeQuery(sql);
//以下只获取了行
// while(rs.next()){
// String num=rs.getString("number");
// String name=rs.getString("name");
// String sex=rs.getString("sex");
// int age=rs.getInt("age");
// System.out.println(num+"-"+name+"-"+sex+"-"+age);
// }
//获取列
int count=rs.getMetaData().getColumnCount();
while(rs.next()){//遍历行
for(int i=1;i<=count;i++)
{
//getString()和getObject()相当于通用
System.out.print(rs.getString(i));
if(i<count)
{
System.out.print(",");
}
}
System.out.println();
}
}catch(SQLException e){
//System.out.println(e.getMessage());
throw new RuntimeException(e);
}finally{
rs.close();
stmt.close();
conn.close();//必须关!
}
}
}
java-JDBC:数据库的增删改查范例
最新推荐文章于 2023-11-22 16:12:47 发布