JAVA学习笔记026——Java操作MySQL数据库实例(Statement)

第一次调试成功:

数据表student:

容易出故障的几个点,

1.import不全

2.mysql时区问题。

2020-7-3补充查询功能

public static void update()   //增删改功能

public static void query()     //查询功能

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.cj.protocol.Resultset;

public class JDBCDemo{
	private static final String URL="jdbc:mysql://localhost:3306/lds?serverTimezone=UTC";
	private static final String USERNAME="root";
	private static final String PWD="prolific";
//  


	public static void update() {//增删改
		Connection connection = null;
		Statement stmt =null;
		ResultSet rs=null;
		try {
			//  a.导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");//加载具体的驱动类
			//      b.与数据库建立连接
			connection = DriverManager.getConnection(URL,USERNAME,PWD);
			//c.发送sql,执行(增、删、改、查)
			stmt=connection.createStatement();
			String sql="insert into student values(10,'Xiaolingnv',32,'Xiagu') ";
			//String sql="update student set STUNAME='Gaiming' where stuno=2";
			//String sql="delete from student where stuno=4";


			int count = stmt.executeUpdate(sql);  //返回值标是增删改几条数据
			//查询是stmt.executeQuery(sql);
			
			//d.处理结果
			
			if (count>0){
				System.out.println("操作成功!");
			}
			
			
		}  catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(Exception e){
	  e.printStackTrace();
		}
		finally{
			try {
				if (stmt!=null) stmt.close();   //对象.方法
				if (connection!=null) connection.close();			
			}catch(SQLException e) {
				e.printStackTrace();
			}
			
		}
	}	
	
	
	public static void query() {//查询功能
		Connection connection = null;
		Statement stmt =null;
		ResultSet rs=null;
		try {
			//  a.导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");//加载具体的驱动类
			//      b.与数据库建立连接
			connection = DriverManager.getConnection(URL,USERNAME,PWD);
			//c.发送sql,执行(增、删、改、查)
			stmt=connection.createStatement();
			String sql="select stuno,stuname from student";
			rs=stmt.executeQuery(sql);
			//查询是stmt.executeQuery(sql);
			//int count = stmt.executeUpdate(sql);  //返回值标是增删改几条数据
			
			//d.处理结果
			/*
			if (count>0){
				System.out.println("操作成功!");
			}
			*/
			while(rs.next()) {
//				int sno=((ResultSet) rs).getInt("stuno");
//				String sname=rs.getString("stuname");
				int sno=rs.getInt(1);
				String sname=rs.getString(2);
				System.out.println(sno+"------"+sname);
				
			}
			
		}  catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(Exception e){
	  e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null) rs.close();
				if (stmt!=null) stmt.close();   //对象.方法
				if (connection!=null) connection.close();			
			}catch(SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
	

	public static void main(String[] args){
		update();
		query();
		
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42844704/article/details/106970930