Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 为什么不能连入数据库?
wppdennis





发贴: 7
积分: 0
于 2006-05-08 10:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

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();
}
}


why edited on 2006-05-09 07:22

作者 Re:为什么不能连入数据库? [Re:wppdennis]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-05-09 07:23 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
> catch(SQLException ee){}
Please don't swallow the exception but show us what it is.Smile



作者 Re:为什么不能连入数据库? [Re:wppdennis]
K_P_exciting





发贴: 9
积分: 0
于 2006-05-19 21:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你是通过数据源的方式连接数据库的啊!好像那个
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目录中。


why edited on 2006-05-20 11:03


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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