我有这段代码,我遇到了一个问题,使用 java netbeans 从 MySQL 数据库读取信息,数据库 varchar 中的数据类型
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Scanner;
public class NewDataBase {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb","root", "password");
Statement stmt = (Statement) con.createStatement();
System.out.println("Enter name");
String name =sc.next();
String SQL = "select * from list where Name ='"+ name + "'";
ResultSet rs = stmt.executeQuery(SQL);
System.out.print("Name: " + rs.getNString("Name"));
System.out.print("Number: " + rs.getNString("Number"));
// while(rs.next())
// {
// System.out.print("Name: " + rs.getNString("Name"));
// System.out.print("Number: " + rs.getNString("Number"));
//}
}catch(Exception e){
System.out.print("Error:" + e.getMessage());
}
}
}
错误:当字段的字符集不是 UTF-8 时,无法调用 getNString()