Topic: 为什么不能连入数据库?

  Print this page

1.为什么不能连入数据库? Copy to clipboard
Posted by: wppdennis
Posted on: 2006-05-08 10:22


import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.net.*;
class f1 extends Frame implements ActionListener
{
Label label;
TextField text1;
TextArea text2;
Button button;
Panel panel1,panel2;
f1()
{
super("英汉小词典");
setBackground(Color.cyan);
setBounds(100,120,800,600);
setVisible(true);
text1=new TextField(15);
text2=new TextArea(5,10);
button=new Button("OK");
label=new Label("输入要查询的单词");
panel1=new Panel();
panel2=new Panel();
add(panel1,"North");add(panel2,"South");add(text2,"Center");
panel1.add(label);panel1.add(text1);
panel2.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
text2.setText("查询结果");
try{Do();}
catch(SQLException ee){}
}
}
public void Do() throws SQLException
{
String cname,ename;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
Connection con=DriverManager.getConnection("jdbc.odbc:XYZ","","");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT * FROM chaxun ");
while (rs.next())
{
ename=rs.getString("英文");
cname=rs.getString("中文");
if(ename.equals(text1.getText()))
{
text2.append('\n'+cname);
}
}

}
}
public class Data
{
public static void main(String arg[])
{
f1 f=new f1();
}
}

2.Re:为什么不能连入数据库? [Re: wppdennis] Copy to clipboard
Posted by: why
Posted on: 2006-05-09 07:23

> catch(SQLException ee){}
Please don't swallow the exception but show us what it is.Smile

3.Re:为什么不能连入数据库? [Re: wppdennis] Copy to clipboard
Posted by: K_P_exciting
Posted on: 2006-05-19 21:20

你是通过数据源的方式连接数据库的啊!好像那个
DriverManager.getConnection("jdbc.odbc:XYZ","","");
catch(SQLException e) {…………}忘了。
给你一个用JDBC驱动连接的例子:

// dbTest.java -- Connect to SQL Server 2000!
import java.sql.*;

public class dbTest {
public static void main(String args[]) {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user ="sa"; // user name
String password = "sqlserversaps"; // password
String sqlStr = "select CustomerID, CompanyName, ContactName from Customers";

try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// get Connection
Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement(); // create Statement
ResultSet rs = st.executeQuery(sqlStr);

while(rs.next()) {
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(outputFormat(rs.getString("CompanyName"), 40));
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
}
catch(Exception err) {
err.printStackTrace(System.out);
}
}

// function for output format
public static String outputFormat(String s, int x) {
if(s.length() >= x)
return s;
StringBuffer buf = new StringBuffer();
buf.append(s);
for(int i = s.length(); i < x; i++)
buf.append(" ");
return buf.toString();
}
}

说明:运行这个例子前,确认你安装了SQL Server 2000 SP4补丁,并且安装了JDBC驱动,并把三个包复制到了JDK和JRE下面的ext目录中。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923