Java--通过jdbc访问mysql数据库(mysql v8.0.11)

由于mysql的更新,原来的连接数据库方法改变了

参考:http://www.cnblogs.com/rainbow70626/p/9005852.html

package demo;

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

/**
 * 
 * @描述:jdbc连接mysql数据库
 * 1-拷贝jar包
 * 2-获取连接
 * 3-操作数据库
 * 4-关闭连接
 * @作者: zjw
 * @时间: 2018年7月18日 下午3:39:11
 */
public class SelectMain
{
	public static void main(String[] args)
	{
		String driver = "com.mysql.cj.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/myfirst?useSSL=false&serverTimezone=UTC";
		String user = "root";
		String password = "123456";
		String sql = "select * from student";
		Connection conn = null ;
		Statement stat = null ;
		ResultSet rs = null ;
		
		try
		{
			//加载驱动
			Class.forName(driver);
//			Driver driver = new Driver();
			
			//获取连接
			conn = DriverManager.getConnection(url, user, password);
			//操作数据库
			stat = conn.createStatement();
			//执行sql语句 返回一个结果集(游标)
			rs = stat.executeQuery(sql);
			
			while(rs.next())
			{
				int id = rs.getInt(1);
				String name = rs.getString("name");
				int age = rs.getInt("age");
				double chinese = rs.getDouble("chinese");
				Date createTime = rs.getTimestamp("createTime");
				System.out.println(id+"\t" +name+"\t"+age+"\t"+chinese+"\t"+createTime+"\t");
				
			}
			
		} catch ( ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}finally 
		{
			try
			{
				if(rs != null)
				{
					rs.close();
					rs = null ;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(stat != null)
				{
					stat.close();
					stat = null ;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(conn != null)
				{
					conn.close();
					conn = null ;
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			
		}
	}
}

结果:

1	张三	20	99.0	2017-12-09 18:04:57.0	
2	李四	23	60.0	2017-12-09 18:08:16.0	
3	王姐	34	78.0	2017-12-09 18:09:32.0	
4	张一	21	68.0	2017-12-09 18:09:06.0	
5	刘洋	18	88.0	2017-12-09 18:11:56.0	
6	张二	21	80.0	2017-12-09 18:09:44.0	
7	赵四	21	98.0	2017-12-09 18:10:14.0	
8	王五	24	92.0	2017-12-09 18:10:31.0	
9	张四	21	21.0	2017-12-09 18:10:20.0	
10	张五	21	21.0	2017-12-09 18:10:51.0	
11	赵照	23	90.0	2017-12-09 18:10:43.0	
12	赵匡胤	24	87.0	2017-12-09 18:11:14.0	
13	李元霸	34	78.0	2017-12-09 18:11:15.0	
14	朱由校	32	87.0	2017-12-09 18:11:38.0	
15	赵好	18	77.0	2017-12-09 18:12:33.0	
16	朱熹	62	66.0	2017-12-09 18:11:41.0	
17	刘备	26	89.0	2017-12-09 18:11:49.0	
18	岳飞	1	87.0	2017-12-09 18:12:21.0	
19	王琦	28	84.0	2017-12-09 18:12:22.0	
20	赵高	56	77.0	2017-12-09 18:12:27.0	
21	赵飞燕	23	87.0	2017-12-09 18:12:44.0	
22	朱杰	24	78.0	2017-12-09 18:12:53.0	
23	李将	67	99.0	2017-12-09 18:13:49.0	
24	朱祁镇	37	88.0	2017-12-09 18:16:26.0	
25	李斯	78	77.0	2017-12-09 18:13:06.0	
26	赵佶	42	87.0	2017-12-09 18:13:25.0	
27	天皇	68	88.0	2017-12-09 18:13:36.0	
28	诗涵	21	98.0	2017-12-09 18:13:40.0	
29	赵构	87	81.0	2017-12-09 18:13:45.0	
30	张震	21	87.0	2017-12-09 18:14:06.0	
31	李季	66	66.0	2017-12-09 18:14:29.0	
32	碧昂斯	56	88.0	2017-12-09 18:14:11.0	
33	赵四	36	99.0	2017-12-09 18:14:44.0	
34	刘畅	24	78.0	2017-12-09 18:14:35.0	
35	李明	22	44.0	2017-12-09 18:14:59.0	
36	朱翊钧	27	88.0	2017-12-09 18:14:58.0	
37	尚志	35	67.0	2017-12-09 18:14:59.0	
38	小沈阳	36	55.0	2017-12-09 18:15:11.0	
39	李宁	14	66.0	2017-12-09 18:15:32.0	
40	空空空	0	11.0	null	

 

 

 

posted @ 2018-07-18 16:39  雨中遐想  阅读(5)  评论(0编辑  收藏  举报  来源