当前位置:编程学习 > JAVA >>

求大侠指点:在eclipse里面用jdbc连接数据库出现问题

我写的代码:

try{
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   
   }
   catch(Exception e)
   {
   
   }
try{
url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=query";

user="sa";
pswd="sa";
select="select * from department where departmentName='"+departname+"')";
conn=DriverManager.getConnection(url,user,pswd);
sql=conn.createStatement();
re=sql.executeQuery(select);
while(re.next())
{
out.print("department is located:");
out.print("("+re.getInt(5)+","+re.getInt(6)+")");

}
conn.close();


   }
   catch(SQLException ee)
{

out.print(ee);
}


数据库名字为:query,表名为:departtment


运行结果在表单的下面出现了这样的问题:
com.microsoft.sqlserver.jdbc.SQLServerException: ')' 附近有语法错误

这是什么问题啊? --------------------编程问答--------------------  你那个sql多了 一个select="select * from department where departmentName=’"+departname+"‘";这样就对了 --------------------编程问答-------------------- 非常感谢,没有出现这个问题了 --------------------编程问答-------------------- 可以查询了,但是查询却没有什么结果,不知道是怎么回事?能帮我看下吗?


这是所有的代码:
SelectDepartment.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
    <%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Show departments and transactions</title>
</head>
<body>
<form action ="" method = post name=form>

查找部门<input type ="text" name="departname" >
</input>
<input type ="submit" value="查询" name = "submit"></input>

</form>
<%
   String  departname = request.getParameter("departname");
   String result="";
   String url;
   String user;
   String pswd;
   String select;
   Connection conn;
   Statement sql;
   ResultSet re;
   
   try{
   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
   
   }
   catch(Exception e)
   {
   
   }
try{
url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=query";

user="sa";
pswd="sa";
select="select * from department where departmentName='"+departname+"'";
conn=DriverManager.getConnection(url,user,pswd);
sql=conn.createStatement();
re=sql.executeQuery(select);
while(re.next())
{
out.print("department is located:");
out.print("("+re.getInt(5)+","+re.getInt(6)+")");

}
conn.close();


   }
   catch(SQLException ee)
{

out.print(ee);
}
   
%>
</body>
</html> --------------------编程问答-------------------- 手动查询下看可有结果。
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,