求救Java用传sql语句进行数据库查询出错
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;impor...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class dataopen {
public void sqlopen(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=nicedata","xx", "xxxx");
if(conn==null){System.out.println("数据加载失败");}
else{}
}catch (Exception ex){
System.out.println(ex.getMessage());
}
}
//查
public List<listtry> split(String sql) throws SQLException{
List<listtry> list=new ArrayList<listtry>();
Connection conn=DriverManager.getConnection("jdbc:sqlserver://JH2012:1433;DatabaseName=pubs","sa", "hack");
Statement stc=conn.createStatement();
ResultSet n=stc.executeQuery(sql);
while(n.next()){
listtry nn=new listtry();
nn.setsId(n.getString(1));
nn.setSname(n.getString(2));
nn.setSage(n.getString(3));
nn.setSsex(n.getString(4));
nn.setSquestion(n.getString(6));
nn.setSanser(n.getString(7));
nn.setSadreess(n.getString(5));
list.add(nn);
}
stc.close();
n.close();
conn.close();
return list;
}
public static void main(String[] args) throws SQLException {
dataopen nn=new dataopen();
nn.sqlopen();
List<listtry> list=nn.split("select * from stuInfo");
for(listtry au : list){
System.out.println(au.getSname()+"\t"+au.getsId());
}
}
}
执行的时候提示List<listtry> list=nn.split("select * from stuInfo");
Statement stc=conn.createStatement(); 展开
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class dataopen {
public void sqlopen(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=nicedata","xx", "xxxx");
if(conn==null){System.out.println("数据加载失败");}
else{}
}catch (Exception ex){
System.out.println(ex.getMessage());
}
}
//查
public List<listtry> split(String sql) throws SQLException{
List<listtry> list=new ArrayList<listtry>();
Connection conn=DriverManager.getConnection("jdbc:sqlserver://JH2012:1433;DatabaseName=pubs","sa", "hack");
Statement stc=conn.createStatement();
ResultSet n=stc.executeQuery(sql);
while(n.next()){
listtry nn=new listtry();
nn.setsId(n.getString(1));
nn.setSname(n.getString(2));
nn.setSage(n.getString(3));
nn.setSsex(n.getString(4));
nn.setSquestion(n.getString(6));
nn.setSanser(n.getString(7));
nn.setSadreess(n.getString(5));
list.add(nn);
}
stc.close();
n.close();
conn.close();
return list;
}
public static void main(String[] args) throws SQLException {
dataopen nn=new dataopen();
nn.sqlopen();
List<listtry> list=nn.split("select * from stuInfo");
for(listtry au : list){
System.out.println(au.getSname()+"\t"+au.getsId());
}
}
}
执行的时候提示List<listtry> list=nn.split("select * from stuInfo");
Statement stc=conn.createStatement(); 展开
1个回答
展开全部
nn.sqlopen();执行通过了嘛?
如果通过了,希望你的dataopen类改造一下,把connection加载到静态变量里面去
private Connection conn ;
public void sqlopen(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=nicedata","xx", "xxxx");
if(conn==null){System.out.println("数据加载失败");}
else{}
}catch (Exception ex){
System.out.println(ex.getMessage());
}
}
在split中直接使用conn即可
如果通过了,希望你的dataopen类改造一下,把connection加载到静态变量里面去
private Connection conn ;
public void sqlopen(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=nicedata","xx", "xxxx");
if(conn==null){System.out.println("数据加载失败");}
else{}
}catch (Exception ex){
System.out.println(ex.getMessage());
}
}
在split中直接使用conn即可
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |