我用的是微软的JDBC驱动器(Microsoft SQL Server 2000 Driver for JDBC),已经在classpath环境变量中设置了lib下三个.jar文件.源程序如下:
package aa;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.microsoft.*;public class Insert extends JFrame implements ActionListener
{
JPanel pane;
JLabel label_a,label_b,label_c;
JTextField text;
JComboBox box;
JList list;
JButton button;
private String age[] = {"1","2","3","4"};
private String sex[] = {"female","male"}; private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
private String user = "sa";
private String password = ""; public Insert(){
super("insert data frame");
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
}catch(Exception e1){
System.out.println("aa"+e1);
} pane = new JPanel();
setContentPane(pane);
setDefaultCloseOperation(EXIT_ON_CLOSE); label_a = new JLabel("name:");
label_b = new JLabel("age:");
label_c = new JLabel("sex:"); text = new JTextField(10);
box = new JComboBox(age);
list = new JList(sex);
button = new JButton("submit"); button.addActionListener(this); pane.add(label_a); pane.add(text);
pane.add(label_b); pane.add(box);
pane.add(label_c); pane.add(list);
pane.add(button); setSize(150,250);
setVisible(true);
} public void actionPerformed(ActionEvent evt){
Object obj = evt.getSource();
String temp = "insert M values ('"+text.getText().trim()+"',"+Integer.parseInt((String)box.getSelectedItem())+",'"+(String)list.getSelectedValue()+"')";
if (button == obj){
try{
Connection con = DriverManager.getConnection(url,user,password);
Statement state = con.createStatement();
int i = state.executeUpdate(temp);
if ( i == 1 ){
System.out.println("succes!");
}
}catch(Exception e){
System.out.println("bb"+e);
}
}
} public static final void main(String args[]){
new Insert();
}
};可是编译的时候总是报如下错误:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\chenyoudi>d:
D:\>javac Insert.java -d .
Insert.java:6: package com.microsoft does not exist
import com.microsoft.*;
^
1 error请那位高手帮忙解决一下,不慎感激!
package aa;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.microsoft.*;public class Insert extends JFrame implements ActionListener
{
JPanel pane;
JLabel label_a,label_b,label_c;
JTextField text;
JComboBox box;
JList list;
JButton button;
private String age[] = {"1","2","3","4"};
private String sex[] = {"female","male"}; private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
private String user = "sa";
private String password = ""; public Insert(){
super("insert data frame");
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
}catch(Exception e1){
System.out.println("aa"+e1);
} pane = new JPanel();
setContentPane(pane);
setDefaultCloseOperation(EXIT_ON_CLOSE); label_a = new JLabel("name:");
label_b = new JLabel("age:");
label_c = new JLabel("sex:"); text = new JTextField(10);
box = new JComboBox(age);
list = new JList(sex);
button = new JButton("submit"); button.addActionListener(this); pane.add(label_a); pane.add(text);
pane.add(label_b); pane.add(box);
pane.add(label_c); pane.add(list);
pane.add(button); setSize(150,250);
setVisible(true);
} public void actionPerformed(ActionEvent evt){
Object obj = evt.getSource();
String temp = "insert M values ('"+text.getText().trim()+"',"+Integer.parseInt((String)box.getSelectedItem())+",'"+(String)list.getSelectedValue()+"')";
if (button == obj){
try{
Connection con = DriverManager.getConnection(url,user,password);
Statement state = con.createStatement();
int i = state.executeUpdate(temp);
if ( i == 1 ){
System.out.println("succes!");
}
}catch(Exception e){
System.out.println("bb"+e);
}
}
} public static final void main(String args[]){
new Insert();
}
};可是编译的时候总是报如下错误:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\chenyoudi>d:
D:\>javac Insert.java -d .
Insert.java:6: package com.microsoft does not exist
import com.microsoft.*;
^
1 error请那位高手帮忙解决一下,不慎感激!
给分!!!!!!!!!!!!
你现在的问题是编译错误
还是类路径的问题(路径包括空格?),将3个jar文件考到一个简单点的目录,如c:\lib
再设置classpath,或者在命令行指定
javac -cp c:\lib\aa.jar1;c:\lib\aa.jar2; Insert.java -d .
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
已经指明了包.
java 的import不能第归的倒入类
谁跟你说是必须的
像这样classpath=c:\...\xxx.jar;
只是在加载驱动的地方改成了sun.jdbc.odbc.JdbcOdbcDriver,程序没有抱错,也有运行结果
但是还是报同样的错误呀
JDBC-ODBC桥 效率比较低,现在用的少