﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-zhangy</title><link>http://www.blogjava.net/zhangy/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 17 Apr 2026 23:49:24 GMT</lastBuildDate><pubDate>Fri, 17 Apr 2026 23:49:24 GMT</pubDate><ttl>60</ttl><item><title>利用Jsvc把Java程序嵌入到Linux服务中去</title><link>http://www.blogjava.net/zhangy/archive/2008/08/01/219310.html</link><dc:creator>zhangy</dc:creator><author>zhangy</author><pubDate>Fri, 01 Aug 2008 05:04:00 GMT</pubDate><guid>http://www.blogjava.net/zhangy/archive/2008/08/01/219310.html</guid><wfw:comment>http://www.blogjava.net/zhangy/comments/219310.html</wfw:comment><comments>http://www.blogjava.net/zhangy/archive/2008/08/01/219310.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhangy/comments/commentRss/219310.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhangy/services/trackbacks/219310.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt"><span class="b"><span style="font-size: 10pt">&nbsp;<span style="font-size: 8pt"><span class="b">来源：赛迪网</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="b">作者：korn</span> </span></span></span></span>
<table class="p11" cellspacing="0" cellpadding="0" width="560" border="0">
    <tbody>
        <tr>
            <td style="word-wrap: break-word" align="left">
            <p class="a14c" style="text-indent: 2em"><span style="font-size: 12pt"></span><span class="a14c" id="zoom">
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">在linux上以服务的方式启动java程序 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">1.安装jsvc </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">在tomcat的bin目录下有一个jsvc.tar.gz的文件,进入tomcat的bin目录下 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#tar xvfz jsvc.tar.gz </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#cd jsvc-src </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#sh support/buildconf.sh </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#chmod 755 configure </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#./configure --with-java=/usr/local/java (改成你的JDK的位置) </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#make </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">2.编写服务启动类 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <center><ccid_nobr>
            <table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
                <tbody>
                    <tr>
                        <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
                        <pre><ccid_code><span style="font-size: 12pt"><span style="font-size: 10pt">package com.sohu.jsvc.test;
                        public class TestJsvc {
                        public static void main(String args[]) {
                        System.out.println("execute main method!");
                        }
                        public void init() throws Exception {
                        System.out.println("execute init method！");
                        }
                        public void init(String[] args) throws Exception{
                        System.out.println("execute init(args) method");
                        }
                        public void start() throws Exception {
                        System.out.println("execute start method！");
                        }
                        public void stop() throws Exception {
                        System.out.println("execute stop method！");
                        }
                        public void destroy() throws Exception{
                        System.out.println("execute destroy method!");
                        }
                        }</ccid_code></span></span></pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </ccid_nobr></center>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">main方法可以去掉，但是init(String[] args),start(),stop(),destroy()方法不能少，服务在启动时会先调用init(String[] args)方法 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">然后调用start()方法，在服务停止是会首先调用stop()方法，然后调用destroy() 方法. </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">3.把这个类打包成testjsvc.jar 放到/test目录下 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">4.编写启动服务的脚本 myjsvc </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <center><ccid_nobr>
            <table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
                <tbody>
                    <tr>
                        <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
                        <pre><ccid_code><span style="font-size: 12pt"><span style="font-size: 10pt">#!/bin/sh
                        # myjsvc This shell script takes care of starting and stopping
                        #
                        # chkconfig: - 60 50
                        # description: tlstat stat is a stat data daemon.
                        # processname: myjsvc
                        # Source function library.
                        . /etc/rc.d/init.d/functions
                        RETVAL=0
                        prog="MYJSVC"
                        # jdk的安装目录
                        JAVA_HOME=/usr/java/jdk1.5.0_15
                        #应用程序的目录
                        MYJSVC_HOME=/test
                        #jsvc所在的目录
                        DAEMON_HOME=/usr/local/tomcat5/bin/jsvc-src
                        #用户
                        MYJSVC_USER=root
                        # for multi instances adapt those lines.
                        TMP_DIR=/var/tmp
                        PID_FILE=/var/run/tlstat.pid
                        #程序运行是所需的jar包，commons-daemon.jar是不能少的
                        CLASSPATH=\
                        /test/testjsvc.jar:\
                        /usr/local/tomcat5/bin/commons-daemon.jar:\
                        case "$1" in
                        start)
                        #
                        # Start TlStat Data Serivce
                        #
                        $DAEMON_HOME/jsvc \
                        -user $MYJSVC_USER \
                        -home $JAVA_HOME \
                        -Djava.io.tmpdir=$TMP_DIR \
                        -wait 10 \
                        -pidfile $PID_FILE \
                        #控制台的输出会写到tlstat.out文件里
                        -outfile $MYJSVC_HOME/log/myjsvc.out \
                        -errfile '&amp;1' \
                        -cp $CLASSPATH \
                        #服务启动类
                        com.sohu.jsvc.test.TestJsvc
                        #
                        # To get a verbose JVM
                        #-verbose \
                        # To get a debug of jsvc.
                        #-debug \
                        exit $?
                        ;;
                        stop)
                        #
                        # Stop TlStat Data Serivce
                        #
                        $DAEMON_HOME/jsvc \
                        -stop \
                        -pidfile $PID_FILE \
                        com.sohu.jsvc.test.TestJsvc
                        exit $?
                        ;;
                        *)
                        echo "Usage myjsvc start/stop"
                        exit 1;;
                        esac</ccid_code></span></span></pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            </ccid_nobr></center>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">5. 把myjsvc文件拷贝到/etc/init.d/目录下 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">6. #chmod -c 777 /etc/init.d/myjsvc </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">7. 添加服务 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#chkconfig --add myjsvc </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#chkconfig --level 345 myjsvc on </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">8. 完成，启动服务 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#service myjsvc start </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">你可以从/test/log/myjsvc.out文件里看到如下信息: </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">execute init(args) method </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">execute start method </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">#service myjsvc stop </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">你会发现/test/log/myjsvc.out文件里会增加如下信息 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">execute stop method </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">execute destroy method </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">并且在系统重启时会自动启动myjsvc服务 </span></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"></span>
            <p style="text-indent: 2em"><span style="font-size: 12pt"><span style="font-size: 10pt">好了，一个简单的 liunx服务就写好了，你可以在TestJsvc的init(),start(),stop(),destroy()方法里添加你的业务，做你想做的事。 </span></span></p>
            </span></td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/zhangy/aggbug/219310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhangy/" target="_blank">zhangy</a> 2008-08-01 13:04 <a href="http://www.blogjava.net/zhangy/archive/2008/08/01/219310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用Java程序连接各种数据库</title><link>http://www.blogjava.net/zhangy/archive/2008/08/01/219290.html</link><dc:creator>zhangy</dc:creator><author>zhangy</author><pubDate>Fri, 01 Aug 2008 03:58:00 GMT</pubDate><guid>http://www.blogjava.net/zhangy/archive/2008/08/01/219290.html</guid><wfw:comment>http://www.blogjava.net/zhangy/comments/219290.html</wfw:comment><comments>http://www.blogjava.net/zhangy/archive/2008/08/01/219290.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhangy/comments/commentRss/219290.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhangy/services/trackbacks/219290.html</trackback:ping><description><![CDATA[<span class="a14c" id="zoom" style="font-size: 12pt"><span class="b"><span style="font-size: 8pt"><span class="a14c" id="zoom"><span class="b">来源：赛迪网</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="b">作者：一角天空</span></span></span></span>
<p style="text-indent: 2em">1、Oracle8/8i/9i数据库（thin模式）
<p style="text-indent: 2em">Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
<p style="text-indent: 2em">String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
<p style="text-indent: 2em">String user="test";
<p style="text-indent: 2em">String password="test";
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url,user,password);
<p style="text-indent: 2em">2、DB2数据库
<p style="text-indent: 2em">Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
<p style="text-indent: 2em">String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名
<p style="text-indent: 2em">String user="admin";
<p style="text-indent: 2em">String password="";
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url,user,password);
<p style="text-indent: 2em">3、SQL Server7.0/2000数据库
<p style="text-indent: 2em">Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
<p style="text-indent: 2em">String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
<p style="text-indent: 2em">//mydb为数据库
<p style="text-indent: 2em">String user="sa";
<p style="text-indent: 2em">String password="";
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url,user,password);
<p style="text-indent: 2em">4、Sybase数据库
<p style="text-indent: 2em">Class.forName("com.sybase.jdbc.SybDriver").newInstance();
<p style="text-indent: 2em">String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名
<p style="text-indent: 2em">Properties sysProps = System.getProperties();
<p style="text-indent: 2em">SysProps.put("user","userid");
<p style="text-indent: 2em">SysProps.put("password","user_password");
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url, SysProps);
<p style="text-indent: 2em">5、Informix数据库
<p style="text-indent: 2em">Class.forName("com.informix.jdbc.IfxDriver").newInstance();
<p style="text-indent: 2em">String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
<p style="text-indent: 2em">user=testuser;password=testpassword"; //myDB为数据库名
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url);
<p style="text-indent: 2em">6、MySQL数据库
<p style="text-indent: 2em">Class.forName("org.gjt.mm.mysql.Driver").newInstance();
<p style="text-indent: 2em">//或者Class.forName("com.mysql.jdbc.Driver");
<p style="text-indent: 2em">String url ="jdbc:mysql://localhost/myDB?
<p style="text-indent: 2em">user=soft&amp;password=soft1234&amp;useUnicode=true&amp;characterEncoding=8859_1"
<p style="text-indent: 2em">//myDB为数据库名
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url);
<p style="text-indent: 2em">7、PostgreSQL数据库
<p style="text-indent: 2em">Class.forName("org.postgresql.Driver").newInstance();
<p style="text-indent: 2em">String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名
<p style="text-indent: 2em">String user="myuser";
<p style="text-indent: 2em">String password="mypassword";
<p style="text-indent: 2em">Connection conn= DriverManager.getConnection(url,user,password);
<p style="text-indent: 2em">8、access数据库直连用ODBC的
<p style="text-indent: 2em">Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
<p style="text-indent: 2em">String url="jdbc:odbc:Driver={MicroSoft Access Driver
<p style="text-indent: 2em">(*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
<p style="text-indent: 2em">Connection conn = DriverManager.getConnection(url,"","");
<p style="text-indent: 2em">Statement stmtNew=conn.createStatement() ; </p>
</span>
<img src ="http://www.blogjava.net/zhangy/aggbug/219290.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhangy/" target="_blank">zhangy</a> 2008-08-01 11:58 <a href="http://www.blogjava.net/zhangy/archive/2008/08/01/219290.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>