数据库为:sql server 2000
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:phdbms";
String user = "sa";
String password = "";
con = DriverManager.getConnection(url, user,password);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT top 2 * FROM peccancyinfo");
}
catch(...)
{
...
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:phdbms";
String user = "sa";
String password = "";
con = DriverManager.getConnection(url, user,password);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT top 2 * FROM peccancyinfo");
}
catch(...)
{
...
}
String url = "jdbc:odbc:phdbms";
方式需要改变一下呀?
到本机上找,先是访问非
MS与SUN简直是死对头!
import java.io.*;
import java.net.*;class Applicant implements Serializable
{
String id;
String applicantID;
String applicantName;
String applicantAddress;
String applicantPosition;}class CandidateServer implements Runnable
{
ServerSocket server;
Socket fromClient;
Thread serverThread;
public CandidateServer()
{
try
{
server = new ServerSocket(1001);
serverThread = new Thread(this);
serverThread.start();
}
catch(Exception e)
{
System.out.println("Cannot start the thread" + e);
}
System.out.println("Server started...");
}
public static void main(String args[])
{
new CandidateServer(); } public void run()
{
try
{
while(true)
{
//Listening to the clients request
fromClient = server.accept();
//Creating the connect object
Connect con = new Connect(fromClient);
}
}
catch(Exception e)
{
System.out.println("Cannot listen to the client" + e);
}
}
}//Code for the connect class
class Connect
{
Applicant data;
ObjectInputStream streamFromClient; public Connect(Socket inFromClient)
{
//Retrieving the clients stream
try
{
streamFromClient = new ObjectInputStream(inFromClient.getInputStream());
try
{
//Retrieving the applicant details from the client
data = (Applicant)streamFromClient.readObject();
}
catch(InvalidClassException e)
{
System.out.println("Cannot serialize the applicant class" + e);
}
catch(NotSerializableException e)
{
System.out.println("The object is not serializable" + e);
}
catch(IOException e)
{
System.out.println("Cannot read from the client stream" + e);
}
//Call the submit method
submit();
}
catch(Exception e)
{
System.out.println("Cannot get the client stream" + e);
} }//Code for the submit method
public void submit()
{
try
{ //Code for submiting the applicant details to the //database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con;
con = DriverManager.getConnection("jdbc:odbc:aa","sa",null);
PreparedStatement stat = con.prepareStatement
("Update ApplicantTable set cApplicantName= ? , cApplicantAddress = ? , cApplicantPosition = ? where cApplicantId = ?");
stat.setString(1,data.applicantName);
stat.setString(2,data.applicantAddress);
stat.setString(3,data.applicantPosition);
stat.setString(4,data.applicantID);
stat.executeUpdate();
}
catch(Exception e)
{
System.out.println("Cannot insert the data in the table" + e);
}
}
}
mport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;class Applicant implements Serializable
{
String id;
String applicantID;
String applicantName;
String applicantAddress;
String applicantPosition;
}public class ApplicantApplet extends JApplet
{
//Variable for frame window
static JPanel panel;
Container content; //variables of labels
JLabel labelAppID;
JLabel labelAppName;
JLabel labelAppAddress;
JLabel labelAppPosition; //variables for data entry controls JTextField textAppID;
JTextField textAppName;
JTextField textAppAddress;
JComboBox comboAppPosition; //Button to Accept values JButton buttonAccept; //Layout GridBagLayout gl;
GridBagConstraints gbc;
public void init()
{
// Add the appropriate controls
//Create a panel and add it to the frame gl=new GridBagLayout();
gbc=new GridBagConstraints();
content = getContentPane();
content.setLayout(gl);
//Create and add the appropriate controls //Initializing labels
labelAppID = new JLabel("Applicant ID");
labelAppName = new JLabel("Name");
labelAppAddress = new JLabel("Address");
labelAppPosition = new JLabel("Position");
//Initializing TextField
textAppID = new JTextField(5);
textAppName = new JTextField(30);
textAppAddress = new JTextField(30); buttonAccept = new JButton("Accept"); //Object array that contains the position code and the //Description Object positions[]={
new String("0001"),
new String("0002"),
new String("0003"),
new String("0004"),
new String("0005")
};
comboAppPosition = new JComboBox(positions);//Adding controls for Applicant id gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=5;
gl.setConstraints(labelAppID,gbc);
content.add(labelAppID); gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=5;
gl.setConstraints(textAppID,gbc);
content.add(textAppID);//Adding controls for Applicant name gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=8;
gl.setConstraints(labelAppName,gbc);
content.add(labelAppName); gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=8;
gl.setConstraints(textAppName,gbc);
content.add(textAppName);
//Adding controls for Applicant Address gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=11;
gl.setConstraints(labelAppAddress,gbc);
content.add(labelAppAddress); gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=11;
gl.setConstraints(textAppAddress,gbc);
content.add(textAppAddress);//Adding controls for Applicant Position gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=14;
gl.setConstraints(labelAppPosition,gbc);
content.add(labelAppPosition); gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=14;
gl.setConstraints(comboAppPosition,gbc);
content.add(comboAppPosition);//Adding the accept button
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.gridx=8;
gbc.gridy=11;
gl.setConstraints(buttonAccept,gbc);
content.add(buttonAccept);
validateAction validateButton = new validateAction();
buttonAccept.addActionListener(validateButton);
}//Listener interface implementation for the buttonclass validateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//Extracting source of action
Object obj = evt.getSource();
if(obj == buttonAccept)
{
Applicant data=new Applicant();
data.applicantID = textAppID.getText();
// Retrieving the Applicant Name from the textbox
data.applicantName = textAppName.getText();
//Retrieving the Applicant Address
data.applicantAddress = textAppAddress.getText();
//Retrieving the Position
data.applicantPosition = String.valueOf(comboAppPosition.getSelectedItem());
try
{
Socket toServer;
toServer = new Socket("192.168.0.81",1001);
ObjectOutputStream streamToServer=new ObjectOutputStream(toServer.getOutputStream());
//Sending the data to the server for processing
streamToServer.writeObject((Applicant)data);
streamToServer.close();
}
catch(InvalidClassException e)
{
showStatus("The Applicant class is invalid" + e);
}
catch(NotSerializableException e)
{
showStatus("The object is not serializable" + e);
}
catch(IOException e)
{
showStatus("Cannot write to the server" + e);
}
}
}
}
}
要通过ODBC使用非本机的数据库,必须在本机配置ODBC数据源。JDBC使用URL连接数据库,URL本身带有机器名和端口,他当然是可以连接到远程机器了。
ODBC的数据源就不带任何试图连接远程机器的机器名、管道、断口等信息,当然不能连接到
远程机器了 shixianpeng(大鹏) :你要先搞清楚ODBC的工作原理在想这是怎么回事。不要简单的下结果不好使的结论,
给别人带来误导
<HTML>
<APPLET
CODE = "ApplicantApplet.class"
HEIGHT = 300
WIDTH = 500 >
</APPLET>
</HTML>
to shixianpeng(大鹏) :
不知道,就不要乱下结论,误导别人不好的,