麻烦高手帮我看看代码

kelly7717169 2010-03-23 10:13:42
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class InsertTest {

private static JTextField textField;
public static void main(String[] args) {
//这里加入你的数据库驱动
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注册数据库驱动
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}
JFrame jf = new JFrame();//界面筐体
jf.setSize(495, 371);//界面大小
jf.getContentPane().setLayout(null);//设置布局
final JButton xieruButton = new JButton();//按钮控件
xieruButton.setBounds(139, 215, 118, 28);//按钮位置
//以下是点击按钮后 要做的工作 [也就是插入数据的操作]
xieruButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Connection con;
try {
con = DriverManager.getConnection("192.168.1.17"+ "1433" + "test" + "study"+"sa"+"");
Statement stat = con.createStatement();
stat.executeUpdate("insert into study(id) values (" + textField.getText() +"");
} catch (SQLException e1) {
e1.printStackTrace();
}

}
});
//标签控件
xieruButton.setText("写入数据");
jf.getContentPane().add(xieruButton);

final JLabel label = new JLabel();
label.setBounds(60, 152, 79, 28);
label.setText("请输入数据:");
jf.getContentPane().add(label);

textField = new JTextField();
textField.setBounds(145, 155, 130, 22);
jf.getContentPane().add(textField);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}

}
上面代码哪有问题啊?写不进数据,在eclipse中运行,提示如下:
java.sql.SQLException: No suitable driver found for 192.168.1.171433teststudysa
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at InsertTest$1.actionPerformed(InsertTest.java:34)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
...全文
82 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
kelly7717169 2010-03-23
  • 打赏
  • 举报
回复
不行啊,大哥能不能QQ远程帮我看下啊?
我QQ30384318,麻烦了
你QQ多少啊?
stalendp 2010-03-23
  • 打赏
  • 举报
回复
函数原型如下:

Connection java.sql.DriverManager.getConnection(String url, String user, String password) throws SQLException
Attempts to establish a connection to the given database URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers.

Parameters:
url a database url of the form jdbc:subprotocol:subname
user the database user on whose behalf the connection is being made
password the user's password
Returns:
a connection to the URL
Throws:
SQLException - if a database access error occurs
jimyx123 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 kelly7717169 的回复:]
引用 5 楼 jimyx123 的回复:
把MS下载的JDBC解压后 应该会看到一个HELP目录吧
里面就有samples

我下的是JDBC.exe文件啊
[/Quote]
那去安装到的目录找下
stalendp 2010-03-23
  • 打赏
  • 举报
回复
DriverManager.getConnection("192.168.1.17"+ "1433" + "test" + "study"+"sa"+"");
这里面的url不太正确吧,中间要用“:”隔开吧,自己找个例子看看
kelly7717169 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jimyx123 的回复:]
把MS下载的JDBC解压后 应该会看到一个HELP目录吧
里面就有samples
[/Quote]
我下的是JDBC.exe文件啊
jimyx123 2010-03-23
  • 打赏
  • 举报
回复
把MS下载的JDBC解压后 应该会看到一个HELP目录吧
里面就有samples
kelly7717169 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiaohuanjie 的回复:]
No suitable driver found for 192.168.1.171433teststudysa

你的数据库驱动不合适,用不了

sun.jdbc.odbc.JdbcOdbcDriver
一般很少用SUN提供的数据库驱动,都使用数据库厂商提供的驱动
[/Quote]
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注册数据库驱动

那我要怎么看使用哪种啊?又要怎么样使用了?
kelly7717169 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jimyx123 的回复:]
DriverManager.getConnection("192.168.1.17"+ "1433" + "test" + "study"+"sa"+"");
如果是SQLSERVER的话 可以在MS下个JDBC驱动 里面有例子 看下这连接串是怎样的
[/Quote]
请问怎么看啊?我已经装了啊~!
xiaohuanjie 2010-03-23
  • 打赏
  • 举报
回复
No suitable driver found for 192.168.1.171433teststudysa

你的数据库驱动不合适,用不了

sun.jdbc.odbc.JdbcOdbcDriver
一般很少用SUN提供的数据库驱动,都使用数据库厂商提供的驱动
jimyx123 2010-03-23
  • 打赏
  • 举报
回复
DriverManager.getConnection("192.168.1.17"+ "1433" + "test" + "study"+"sa"+"");
如果是SQLSERVER的话 可以在MS下个JDBC驱动 里面有例子 看下这连接串是怎样的
hy1986521 2010-03-23
  • 打赏
  • 举报
回复
con = DriverManager.getConnection("192.168.1.17"+ "1433" + "test" + "study"+"sa"+"");
这有问题,你的192.168.1.17 和1443 应该是这样的192.168.1.17:1433 明白吗?1433是你的开发端口,要有个:分开的

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧