我能';t使用java netbeans从数据库读取数据
我有这段代码,我遇到了一个问题,使用java netbeans从MySQL数据库中读取信息,数据库varchar中的数据类型我能';t使用java netbeans从数据库读取数据,java,Java,我有这段代码,我遇到了一个问题,使用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 { /** *
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()尝试
rs.getString(“Name”)
而不是rs.getNString(“Name”)尝试rs.getString(“Name”)
而不是rs.getNString(“Name”)
使用:
Properties props = new Properties();
props.put("user", "root");
props.put("password", "password");
props.put("useUnicode", "true");
props.put("useServerPrepStmts", "false"); // use client-side prepared statement
props.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
String url = "jdbc:mysql://localhost/mydb";
Connection connection = DriverManager.getConnection(url, props);
PreparedStatement ps =
connection.prepareStatement("INSERT INTO list (Name, Number) VALUES (?, ?)");
String testNumber = "12345";
String testName = "name1";
pstmt1.setNCharacterStream(1, new StringReader(testName), testName.length());
ps.setNCharacterStream(2, new StringReader(testNumber), testNumber.length());
ps.execute();
也别忘了在中发表评论
if (rs.next()) {
System.out.print("Name: " + rs.getNString("Name"));
System.out.print("Number: " + rs.getNString("Number"));
}
使用:
也别忘了在中发表评论
if (rs.next()) {
System.out.print("Name: " + rs.getNString("Name"));
System.out.print("Number: " + rs.getNString("Number"));
}
是的,最好使用PreparedStatement进行SQL注入和反斜杠、引号等的转义
是的,最好使用PreparedStatement进行SQL注入和反斜杠、引号等的转义。我100%确信这是可行的。这是我从南贝里得到的答案
rs.getString("Name") instead of rs.getNString("Name")
我100%确信这是可行的。这是我从南贝里得到的答案
rs.getString("Name") instead of rs.getNString("Name")
您对SQL注入非常开放。要避免这种情况,请始终使用。您对SQL注入非常开放。为了避免这种情况,请始终使用。错误:在结果sePlease开始之前添加完整的错误堆栈跟踪和更新的代码。一半的信息可能没有帮助。错误:在结果开始之前,请添加完整的错误堆栈跟踪和更新的代码。一半的信息可能没有帮助。请在列表(名称、编号)中插入值(_utf8'aaaaa',_utf8'22222');错误:当字段的字符集不是UTF-8connect to database成功完成时,无法调用getNString()。通过以下语句成功地完成了数据库更新:stmt.executeUpdate(“插入列表(名称、编号)值(_utf8'AB',_utf8'45”);但是在我的程序中从数据库检索数据是不成功的,我认为这是数据库中的数据类型,必须使用什么类型的数据。感谢您的回复。在列表(名称、编号)中插入值(_utf8'aaaaaa',_utf8'22222');错误:当字段的字符集不是UTF-8connect to database成功完成时,无法调用getNString()。通过以下语句成功地完成了数据库更新:stmt.executeUpdate(“插入列表(名称、编号)值(_utf8'AB',_utf8'45”);但是在我的程序中从数据库检索数据是不成功的,我认为这是数据库中的数据类型,必须使用什么类型的数据。谢谢你的回复。