mysql连接jdbc查询代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql连接jdbc查询代码相关的知识,希望对你有一定的参考价值。
package com.simope.test; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class ConnToMySql { public static final String url = "jdbc:mysql://localhost/my_db"; public static final String userName = "root"; public static final String password = "root"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = (Connection) DriverManager.getConnection(url, userName, password); stmt = (Statement) conn.createStatement(); String sql = "select * from sys_user"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println("按照列下标:" + rs.getInt(1)); System.out.println("按照列名:" + rs.getInt("userID")); } } catch (Exception e) { System.out.println("first:" + e); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { System.out.println("second:" + e); } } } }
所用到的jar包:mysql-connector-java-5.1.5-bin.jar
以上是关于mysql连接jdbc查询代码的主要内容,如果未能解决你的问题,请参考以下文章
java使用jdbc连接mysql数据库,表里有记录,但是查询的结果集为空,是啥原因
关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段