我编写了一个程序,它在 mysql 数据库中搜索给定的印地语单词并检索其存储在数据库中的相应英文名称。当我直接在 select 语句中给出印地语单词时,它工作正常,但我想使用变量给它,这样它可能更通用,但我没有得到相同的结果..任何人都可以帮助我解决它..提前谢谢
这是我写的代码
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import com.microsoft.sqlserver.jdbc.*;
import java.sql.*;
public class Example {
public static void main(String[] argv) {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
Connection connection = null;
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setServerName("COMP-PC");
dataSource.setPortNumber(1433);
dataSource.setDatabaseName("concept");
dataSource.setUser("root");
dataSource.setPassword("abc");
try
{
connection = dataSource.getConnection();
connection.setAutoCommit(false);
System.out.println("Connected to server !!!");
Statement select = connection.createStatement();
String var="N'हल्दी'";
ResultSet result = select.executeQuery
("SELECT Name, Id FROM MConcept where CName=N'हल्दी'");
**// When given like the above statement it works fine
("SELECT Name, Id FROM MConcept where CName='"+var+" ' ");
**//This does not give result
System.out.println("Got results:");
while(result.next()) { // process results one row at a time
String Name = result.getString(1);
int ID = result.getInt(2);
System.out.println("name = " + Name);
System.out.println("id = " + ID);
}
select.close();
connection.close();
}
catch (SQLException e)
{
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null)
{
System.out.println("Successfully connected!");
}
else
{
System.out.println("Failed to make connection!");
}
}
}