mysql单元素怎么设置 如何设置mysql( 六 )


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单元素怎么设置的信息别忘了在本站进行查找喔 。

推荐阅读