Java JDBCを使用したMySQLに対する「カウント」クエリの戻りタイプは何ですか?
カウントの戻りタイプは長いです。 Javaステートメントは次のとおりです
rs.next();
long result= rs.getLong("anyAliasName"); まず、サンプルデータベースtest3にいくつかのレコードを含むテーブルを作成します。テーブルを作成するためのクエリは次のとおりです
mysql> create table CountDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.60 sec)
挿入コマンドを使用して、テーブルにいくつかのレコードを挿入します。
クエリは次のとおりです
mysql> insert into CountDemo(Name) values('John');
Query OK, 1 row affected (0.21 sec)
mysql> insert into CountDemo(Name) values('Carol');
Query OK, 1 row affected (0.16 sec)
mysql> insert into CountDemo(Name) values('Bob');
Query OK, 1 row affected (0.19 sec)
mysql> insert into CountDemo(Name) values('David');
Query OK, 1 row affected (0.16 sec) selectステートメントを使用してテーブルのすべてのレコードを表示します。
クエリは次のとおりです
mysql> select *from CountDemo;
以下は出力です
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | David | +----+-------+ 4 rows in set (0.00 sec)
これは、JavaJDBCを使用したMySQLに対する「カウント」クエリのJavaコード戻りタイプです
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ReturnTypeOfCount {
public static void main(String[] args) {
Connection con=null;
Statement st=null;
ResultSet rs=null;
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test3?useSSL=false",
"root","123456");
String yourQuery = "SELECT COUNT(*) AS totalCount FROM CountDemo";
st = con.createStatement();
rs = st.executeQuery(yourQuery);
rs.next();
long result= rs.getLong("totalCount");
System.out.println("Total Count="+result);
} catch(Exception e) {
e.printStackTrace();
}
}
} 以下は出力です
Total Count=4 Here is the snapshot of the output:
-
MySQL JDBCドライバー接続文字列とは何ですか?
MySQLJDBC接続文字列は次のようになります- Class.forName(com.mysql.jdbc.Driver); 上記では、ドライバーはインターフェースです。 JDBCがアプリケーションサーバーの外部で実行されている場合は常に、クラスDriverManagerが接続を確立します。 DriverManagerクラスは次のとおりです- conn = (Connection) DriverManager.getConnection(jdbc:mysql://localhost/yourdatabaseName,”yourRootName,yourPassword); ここ
-
MySQLのsmallintに相当するJavaは何ですか?
shortは、MySQLのsmallintに相当します。 Java shortは-32768〜32767の範囲の2バイトを取りますが、MySQLsmallintも同じ範囲の2バイトを取ります。 これがJavaでのshortのデモコードです- public class SmallIntAsShortDemo { public static void main(String[] args) { short value = 32767; System.out.println(value);