This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16306|Reply: 0

[Java Source Code] Java calls the Properties configuration file source code

[Copy link]
Posted on 11/21/2014 1:12:12 PM | | |
Java source code:

[mw_shl_code=java,true]import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;


public class file_ini {

        /**
         * @param args
         * IT Forum:www.itsvse.com
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Connection conn=null;
                CallableStatement cstmt=null;
               
                try {
                        Properties p=new Properties();
                        p.load(file_ini.class.getResourceAsStream("/jdbc.properties"));
                        String dr=p.getProperty("driver");
                        String url=p.getProperty("url");
                        String username=p.getProperty("username");
                        String pwd=p.getProperty("password");
                       
                        Set the driver type
                        Class.forName(dr);
                        Create a connection object
                        conn=DriverManager.getConnection(
                                        url, username, pwd);
                        sql statement
                        String sql="{call proc_test(?,?,?)}";
                        Create an object that executes the stored procedure
                        cstmt=conn.prepareCall(sql);
                        Set the parameters of the stored procedure
                        cstmt.setInt(1, 20);
                        cstmt.setInt(2, 30);
                        cstmt.registerOutParameter(3, Types.INTEGER);
                        Execute stored procedures
                        cstmt.execute();
                        Get the return value
                        int num=cstmt.getInt(3);
                        System.out.println(num);
                       
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }

}
[/mw_shl_code]

jdbc.properties source code:

[mw_shl_code=java,true]driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc\:sqlserver\://localhost\:1433; database\=sqlDB
username=sa
password=123
[/mw_shl_code]

Database source code:

[mw_shl_code=sql,true]--Create database
create database sqlDB
go
--switch databases
use sqlDB
go
--Create stored procedures
create proc proc_test
@a int,
@b int,
@num int output
as
set @num=@a+@b

--Execute stored procedures
declare @add int
exec proc_test 1,2,@add output
print @add
[/mw_shl_code]

存储过程和配置文件.rar (287.1 KB, Number of downloads: 0, Selling price: 2 Grain MB)






Previous:How do Java use stored procedures? Java executes stored procedure source code
Next:When your talent can't support your ambitions, then you should calm down and learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com