System.out.println("Driver " + dma.getDriverName());
} catch (Exception e) {
System.out.println(e);
}
try {
Statement stmt = con.createStatement();
results = stmt.executeQuery("select * from 客户;");
ResultSetMetaData resultMetaData = https://www.04ip.com/post/results.getMetaData();
int cols = resultMetaData.getColumnCount();
String resultRow = "";
for (int i = 1; icols; i++) {
resultRow += resultMetaData.getColumnName(i) + ";";
}
System.out.println(resultRow);
while (results.next()) {
resultRow = "";
for (int i = 1; icols; i++) {
try {
resultRow += results.getString(i) + ";";
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
System.out.println(resultRow);
}
} catch (Exception e) {
System.out.println("query exception");
} finally {
results.close();
}
}
}
补充高级内容
关于调用SQLServer存储过程的例子:(用到了我们开发的数据库连接类)
CREATE PROCEDURE [dbo].[sp_getStudentByName](@name char(10))
AS
Select * from Students where [Name]=@name
GO
DbObject DbO = new DbObject(new SqlServerConnectionFactory("localhost",
1433, "demo", "sa", ""));
Connection con = DbO.getConnection();
CallableStatement pstmt = null;
System.out.println("TestDB1()............");
/* try {
pstmt = con.prepareCall("{call sp_getStudentById(?)}");
pstmt.setInt(1, 1);
}*/
try {
pstmt = con.prepareCall("{call sp_getStudentByName(?)}"); //注意参数如何传递
pstmt.setString(1, "Tom");
}
……
使用输出参数:
CREATE PROCEDURE [dbo].[sp_insertStudent](@name char(10),@age int,@id int OUTPUT) AS
insert into Students([Name],[Age]) values (@name,@age)
select @id=@@IDENTITY
GO
try {
pstmt = con.prepareCall("{call sp_insertStudent(?,?,?)}");
pstmt.setString(1, "zengqingsong");
pstmt.setInt(2, 22);
pstmt.registerOutParameter(3, Types.INTEGER);
pstmt.executeUpdate();
int id = pstmt.getInt(3);
System.out.println(id);
}
使用返回参数的例子:
CREATE PROCEDURE [dbo].[sp_insertStudent](@name char(10),@age int,@id int OUTPUT) AS
insert into Students([Name],[Age]) values (@name,@age)
select @id=@@IDENTITY –测试输出参数
return 30 –测试返回30
GO
try {
pstmt = con.prepareCall("{?=call sp_insertStudent(?,?,?)}");
pstmt.setString(2, "zengqingsong");
pstmt.setInt(3, 22);
pstmt.registerOutParameter(4, Types.INTEGER);
pstmt.registerOutParameter(1, Types.INTEGER);
int ret = pstmt.executeUpdate(); //执行影响的行数
int ret2 = pstmt.getInt(1); //返回参数(输出参数)
int id = pstmt.getInt(4); //输出参数
System.out.println(ret);
System.out.println(ret2);
System.out.println(id);
【mysql单元素怎么设置 如何设置mysql】mysql单元素怎么设置的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于如何设置mysql、mysql单元素怎么设置的信息别忘了在本站进行查找喔 。
推荐阅读
- apimfc和net类库的区别,net api
- 金河田电脑怎么调整硬盘,金河田电脑装系统教程
- 飞行梦想家游戏,飞行梦想家游戏怎么玩
- 直播平台java源代码 直播系统源代码
- Aix7.1编译Redis,编译redis报错
- postgresql时间截取的简单介绍
- vbnet下载 vbnet怎么打开
- 实时查看mysql查询日志的简单介绍
- 腾讯好的角色扮演游戏,腾讯好的角色扮演游戏叫什么