JDBC之注册驱动、建立与数据库之间的连接、查询数据库

package jdbc.demo;

//根据需要引入的类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCdemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//1.注册驱动
			Class.forName("com.mysql.jdbc.Driver");//使用什么驱动连接数据库
			String url="jdbc:mysql://localhost:3306/hahahhha";
			String user="root";//为我们数据库连接的用户名
			String password="root";//为我们数据库设置的密码
			//2.建立连接
			Connection con=DriverManager.getConnection(url,user,password);
			//3.发起请求
			Statement stmt=con.createStatement();
			String sql="select * from student";
			ResultSet rs=stmt.executeQuery(sql);//executeXXX
			//student表示hahahhha数据库下的表
			//4.输出查询结果
			while(rs.next())//指针刚开始指向表头,然后依次往下一行一行的遍历并输出
			{
				System.out.println(rs.getString(1)+","+rs.getString(2));
				//由于我所连接的数据库是学生表,包含学号和姓名两列
				//并且都为varchar类型,因此此处用getString,()中的数字表示第几列
			}
			if(rs!=null) rs.close();
			if(stmt!=null) stmt.close();
			if(con!=null) con.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

但我们上面的代码是有缺陷的,因为Java中若某一步出现了异常,那么后面的代码就不会继续执行,因此可能会由于前面的错误导致我们的 rs,stmt,con无法关闭。因此我们需要把rs,stmt,con三者的关闭放到finally中(不管程序是否发生异常都会继续执行)。但此时又有一个问题,即在try中定义的量只能在try中使用,因此rs,stmt,con是在try中定义的,我们是没有办法在finally中使用的,因此我们要将他们定义为全局变量,全部初始化为null。在finally中执行rs,stmt,con三者的关闭时还需要加上try-catch语句,因此最终为下

package jdbc.demo;

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

public class JDBCdemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection con=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");//使用什么驱动连接数据库
			String url="jdbc:mysql://localhost:3306/hahahhha?useUnicode=true&characterEncoding=UTF8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT";
			String user="root";
			String password="password";
			con=DriverManager.getConnection(url,user,password);
			stmt=con.createStatement();
			String sql="select * from student";
			ResultSet rs=stmt.executeQuery(sql);//executeXXX
			while(rs.next())
			{
				System.out.println(rs.getString(1)+","+rs.getString(2));
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			
				try {
					if(rs!=null) rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				try {
					if(stmt!=null) stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				try {
					if(con!=null) con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}

}

查询数据库时还有第二种方式

while(rs.next())
{
//第一种是直接按照第几列输出,从第1列开始不是第0列.然后根据列的数据类型判断是getString还是getInt或其他。
	System.out.println(rs.getString(1)+","+rs.getString(2));
//第二种是直接通过列的名称来查询输出
	System.out.println(rs.getString("xuehao")+","+rs.getString("xingming");)
}

猜你喜欢

转载自blog.csdn.net/henulmh/article/details/105040523