package abc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.jdbc.PreparedStatement;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/tts?autoReconnect=true&useSSL=false", "root", "root");
String sql = "select * from exceldata limit 0,20";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("fname"));
}
}
}
我在这里创建了一个简单的程序,请帮助并检查它为什么没有在限制0,10上运行。 请不要在这里我使用mysql的新驱动程序(Connector / J 8.0)。
答案 0 :(得分:0)
我无法重现这个问题:
<强>的MySQL 强>:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)
mysql> USE `tts`;
Database changed
mysql> DROP TABLE IF EXISTS `exceldata`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `exceldata` (
-> `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> `fname` VARCHAR(100),
-> `mobile` CHAR(10),
-> `email` VARCHAR(100)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> DESC `exceldata`;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| fname | varchar(100) | YES | | NULL | |
| mobile | char(10) | YES | | NULL | |
| email | varchar(100) | YES | | NULL | |
+--------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> INSERT INTO `exceldata`
-> (`fname`, `mobile`, `email`)
-> VALUES
-> ('fname 1', 'mobile 1', 'email 1'),
-> ('fname 2', 'mobile 2', 'email 2'),
-> ('fname 3', 'mobile 3', 'email 3'),
-> ('fname 4', 'mobile 4', 'email 4'),
-> ('fname 5', 'mobile 5', 'email 5'),
-> ('fname 6', 'mobile 6', 'email 6'),
-> ('fname 7', 'mobile 7', 'email 7'),
-> ('fname 8', 'mobile 8', 'email 8'),
-> ('fname 9', 'mobile 9', 'email 9'),
-> ('fname 10', 'mobile 10', 'email 10'),
-> ('fname 11', 'mobile 11', 'email 11'),
-> ('fname 12', 'mobile 12', 'email 12');
Query OK, 12 rows affected (0.00 sec)
Records: 12 Duplicates: 0 Warnings: 0
mysql> SELECT
-> `id`,
-> `fname`,
-> `mobile`,
-> `email`
-> FROM
-> `exceldata`;
+----+----------+-----------+----------+
| id | fname | mobile | email |
+----+----------+-----------+----------+
| 1 | fname 1 | mobile 1 | email 1 |
| 2 | fname 2 | mobile 2 | email 2 |
| 3 | fname 3 | mobile 3 | email 3 |
| 4 | fname 4 | mobile 4 | email 4 |
| 5 | fname 5 | mobile 5 | email 5 |
| 6 | fname 6 | mobile 6 | email 6 |
| 7 | fname 7 | mobile 7 | email 7 |
| 8 | fname 8 | mobile 8 | email 8 |
| 9 | fname 9 | mobile 9 | email 9 |
| 10 | fname 10 | mobile 10 | email 10 |
| 11 | fname 11 | mobile 11 | email 11 |
| 12 | fname 12 | mobile 12 | email 12 |
+----+----------+-----------+----------+
12 rows in set (0.00 sec)
Java (文件:Test.java
):
package abc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.jdbc.Driver;
import com.mysql.cj.jdbc.PreparedStatement;
public class Test {
public static void main(String[] args) throws ClassNotFoundException,
SQLException,
InstantiationException,
IllegalAccessException {
Driver driver = (Driver) Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/tts?autoReconnect=true&useSSL=false",
System.getProperty("mysql.user"), System.getProperty("mysql.password"));
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("Connection: " + conn.getClass().getName());
System.out.println("JDBC driver: " + driver.getMajorVersion() + "." + driver.getMinorVersion());
String sql = "select * from exceldata limit 0,10";
PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("fname"));
}
}
}
命令行:
$ javac -d . -cp ./mysql-connector-java-8.0.7-dmr-bin.jar Test.java
$ java -cp .:./mysql-connector-java-8.0.7-dmr-bin.jar abc.Test
Java version: 1.8.0_131
Connection: com.mysql.cj.jdbc.ConnectionImpl
JDBC driver: 8.0
fname 1
fname 2
fname 3
fname 4
fname 5
fname 6
fname 7
fname 8
fname 9
fname 10