java webservice数据库操作_Delphi 调用JAVA WebService 操作数据库登录的例子

db.java    连接数据库文件 package com.hyan.db; import java.util.ResourceBundle; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.util.Properties; import java.io.FileInputStream; import java.io.InputStream; import java.io.IOException; public class db {     private static String drivers;     private static String url;     private static String user;     private static String password;     static {     //这里读db.properties文件               //InputStream in = Object.class.getResourceAsStream("src/db.properties");                try {          Properties prop = new Properties();           InputStream in = new FileInputStream("src/db.properties");              prop.load(in);                url = prop.getProperty("url").trim();                drivers = prop.getProperty("drivers").trim();              user = prop.getProperty("user").trim();              password = prop.getProperty("password").trim();          } catch (IOException e) {                e.printStackTrace();            }     }     public static Connection getConnection() throws SQLException {         Connection conn = null;         try {             Class.forName(drivers);             conn = DriverManager.getConnection(url, user, password);         } catch (Exception e) {             e.printStackTrace();         }         if (conn == null) {             throw new SQLException("ctcjz.DBUtils: Cannot get connection.");         }         return conn;     }     public static void close(Connection conn) {         if (conn == null)             return;         try {             conn.close();         } catch (SQLException e) {             System.out.println("ctcjz.DBUtils: Cannot close connection.");         }     }     public static void close(Statement stmt) {         try {             if (stmt != null) {                 stmt.close();             }         } catch (SQLException e) {             System.out.println("ajax.DBUtils: Cannot close statement.");         }     }     public static void close(ResultSet rs) {         try {             if (rs != null) {                 rs.close();             }         } catch (SQLException e) {             System.out.println("ctcjz.DBUtils: Cannot close resultset.");         }     }    } ------------------------------------------------ login.java           操作数据库文件 package com.hyan.login; import java.sql.Connection; import com.hyan.db.*; import com.hyan.service.ServiceHello; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.ResultSet; import javax.xml.ws.Endpoint; public class login { static Connection conn = null; //声明Connection对象 static PreparedStatement pstmt = null; //声明PreparedStatement对象 static ResultSet rs = null; //声明ResultSet对象 static boolean result;      public static void main(String[] args) throws SQLException {     } public boolean login1(String sname) throws SQLException { result=false;         String sql = "select * from users where name='"+sname+"' ";//定义SQL语句                try {             conn = db.getConnection(); //获取数据库连接             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement             rs = pstmt.executeQuery(); //执行查询,返回结果集             while (rs.next()) {                //rs.getString(1);             result=true;             }         } catch (SQLException e) {             System.out.println(e.toString());         } finally {             rs.close();             conn.close(); //关闭连接         }         return result;     } } ------------------------------------------------ ServiceHello.java             WebService文件 package com.hyan.service; import java.sql.SQLException; import javax.jws.WebService; import javax.xml.ws.Endpoint; import com.hyan.login.*; @WebService public class ServiceHello { static boolean result; /** * @param args * @throws SQLException  */ public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub Endpoint.publish("http://localhost:9001/Service/ServiceHello", new ServiceHello()); System.out.println("Service Success!"); } public boolean login0(String name) { result=false; try { login kkk=new login(); if (kkk.login1(name)==true) { result=true; } } catch (Exception e) {             e.printStackTrace();         } return result; } } ------------------------------------------------------------ Unit1.pas uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls, ServiceHello1; var   ServiceHello1: ServiceHello; procedure TForm1.btn5Click(Sender: TObject); var   wsdl1:string; begin   wsdl1 := 'http://localhost:9001/Service/ServiceHello?wsdl';   //连中间层(JAVA)   ServiceHello1:=GetServiceHello(true,wsdl1,nil);   //edt3.Text:=ServiceHello1.getValue('JAVA Service欢迎Delphi来WebService');   if ServiceHello1.login0(edt3.Text) then   ShowMessage('恭喜你,有这个人')   else   ShowMessage('查无此人'); end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值