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;
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;