啥也不说,直接上代码了!
1. package cn.itcast.zx;
2.
3. import java.sql.CallableStatement;
4.
5. import java.sql.Connection;
6.
7. import java.sql.DriverManager;
8.
9. import java.sql.SQLException;
10.
11. import java.sql.Types;
12.
13.
14. public class JdbcTest {
15.
16.
17. /**
18.
19. * @param args
20.
21. */
22.
23. public static void main(String[] args) {
24.
25. // TODO Auto-generated method stub
26.
27. Connection cn = null;
28.
29. CallableStatement cstmt = null;
30.
31. try {
32.
33. //这里最好不要这么干,因为驱动名写死在程序中了
34.
35. Class.forName("com.mysql.jdbc.Driver");
36.
37. //实际项目中,这里应用DataSource数据,如果用框架,
38.
39. //这个数据源不需要我们编码创建,我们只需Datasource ds = context.lookup()
40.
41. //cn = ds.getConnection();
42.
43. cn = DriverManager.getConnection("jdbc:mysql:///test","root","root");
44.
45. cstmt = cn.prepareCall("{call insert_Student(?,?,?)}");
46.
47. cstmt.registerOutParameter(3,Types.INTEGER);
48.
49. cstmt.setString(1, "wangwu");
50.
51. cstmt.setInt(2, 25);
52.
53. cstmt.execute();
54.
55. //get第几个,不同的数据库不一样,建议不写
56.
57. System.out.println(cstmt.getString(3));
58.
59. } catch (Exception e) {
60.
61. // TODO Auto-generated catch block
62.
63. e.printStackTrace();
64.
65. }
66.
67. finally
68.
69. {
70.
71.
72.
73. /*try{cstmt.close();}catch(Exception e){}
74.
75. try{cn.close();}catch(Exception e){}*/
76.
77. try {
78.
79. if(cstmt != null)
80.
81. cstmt.close();
82.
83. if(cn != null)
84.
85. cn.close();
86.
87. } catch (SQLException e) {
88.
89. // TODO Auto-generated catch block
90.
91. e.printStackTrace();
92.
93. }
94.
95. }
96.
97. }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/253274.html