
数据库管理工具可以很直观查询到数据库储存的数据,如phpMyAdmin只可以直接打开MySQL数据库的表,直接查看其中的内容。但是Mssql数据即使用SQLServer数据库管理工具也很难直观地看到表中保存的数据,需要输入命令才行。
首先打开软件,在右侧的对象资源器选中需要查询的mssql数据库,点击菜单栏新建查询,在新打开的窗口输入表名,点击执行,就可以看到表的数据。
给你个思路,可以先将数据存放在一个集合里面。因为集合是不必定义长度的。然后在根据集合长度来定义OBJ数组。给你贴段代码。希望对你有帮助。public static Object[][] slectAll(String SQL){
Object[][] obj2 = null
ArrayList arr = new ArrayList()
Connection conn = DBAccess.getConn()
String sql = SQL
try {
PreparedStatement ps = conn.prepareStatement(sql)
ResultSet rs = ps.executeQuery()
while(rs.next()){
GoodsSell rl = new GoodsSell()
rl.setSellGoods_Id(rs.getString(1))
rl.setSellGoods_Name(rs.getString(2))
rl.setSellGoods_Price(Double.parseDouble(rs.getString(3)))
rl.setSellGoods_Time(rs.getString(4))
rl.setCustomer_Name(rs.getString(5))
rl.setSellGoods_SalesMan(rs.getString(6))
rl.setSell_PaymentWay(rs.getString(7))
rl.setSell_Remark(rs.getString(8))
arr.add(rl)
}
if(rs!=null){
rs.close()
}
if(ps!=null){
ps.close()
}
obj2 = new Object[arr.size()][8]
for(int i = 0i<arr.size()i++){
obj2[i][0] = ((GoodsSell)arr.get(i)).getSellGoods_Id()
obj2[i][1] = ((GoodsSell)arr.get(i)).getSellGoods_Name()
obj2[i][2] = ((GoodsSell)arr.get(i)).getSellGoods_Price()
obj2[i][3] = ((GoodsSell)arr.get(i)).getSellGoods_Time()
obj2[i][4] = ((GoodsSell)arr.get(i)).getCustomer_Name()
obj2[i][5] = ((GoodsSell)arr.get(i)).getSellGoods_SalesMan()
obj2[i][6] = ((GoodsSell)arr.get(i)).getSell_PaymentWay()
obj2[i][7] = ((GoodsSell)arr.get(i)).getSell_Remark()
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}finally{
DBAccess.closeConn()
}
return obj2
} 但是现在jf.getContentPane().add(table)table报错画红线!Exception in thread "main" java.lang.Error: Unresolved compilation problem:
table cannot be resolved 一般先将table放在jscrollpane里面 然后将jscrollpane放在jpanel 里面 最后将jpanel放在jframe。
多表关联查询与单边查询,对前台来说,都只是一个数据集(DataSet)
连接字符串的写法:
string connectString = "Data Source=.Initial Catalog=StudentIntegrated Security=True"
SqlConnection对象:
SqlConnection sqlCnt = new SqlConnection(connectString)
sqlCnt.Open()sqlCnt.Close()
命名空间:System.Data.SqlClient.SqlConnection
返回数据库连接对象,参数字符串。实例化“连接对象”,并打开连接
使用完成后,需要关闭“连接对象”
SqlCommand command = new SqlCommand()
command.Connection = sqlCnt // 绑定SqlConnection对象
实例化一个SqlCommand对象
执行SQLSqlCommand cmd = conn.CreateCommand() //创建SqlCommand对象
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from products = @ID" //sql语句 可以单表也可以多表关联,
cmd.Parameters.Add("@ID", SqlDbType.Int)
cmd.Parameters["@ID"].Value = 1 //给参数sql语句的参数赋值
SqlCommand cmd = conn.CreateCommand()
cmd.CommandType = System.Data.CommandType.StoredProcedure
cmd.CommandText = "存储过程名"
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)