主题:数据库编程
lovejavalovecpp
[专家分:30] 发布于 2007-10-31 18:20:00
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class LoginWindow extends JDialog implements ActionListener{
PreparedStatement preparedStatement = null;
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JLabel l1 = new JLabel("用户名: ");
JLabel l2 = new JLabel("密 码: ");
JTextField t = new JTextField(20);
JPasswordField psw = new JPasswordField(20);
JButton ok = new JButton("确定");
JButton cancel = new JButton("取消");
public LoginWindow(){
//this.setTitle("登陆窗口");
this.setModal(true);
Container container = this.getContentPane();
container.setLayout(new GridLayout(5,1));
p2.add(l1);
p2.add(t);
p3.add(l2);
p3.add(psw);
p4.add(ok);
p4.add(cancel);
container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
container.add(p5);
ok.addActionListener(this);
cancel.addActionListener(this);
setSize(300,200);
this.setVisible(true);
}
public static void main(String[] args) {
new LoginWindow().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == ok){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("Jdbc:mysql://localhost:3306/test","root","pingzheng");
Statement stmt = conn.createStatement();//执行SQL语句
String name = t.getText();
String word = psw.getText();
String sql = "insert into account(username, password) values ('" +name+ "','" +word+ "' )";
stmt.executeQuery(sql);
conn.commit();
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
else if(e.getSource() == cancel){
System.exit(0);
}
}
}
回复列表 (共9个回复)
沙发
lovejavalovecpp [专家分:30] 发布于 2007-10-31 18:21:00
请高手指点一下 插入数据库有问题啊
我用的是mysql数据库
板凳
超然耗儿 [专家分:60] 发布于 2007-10-31 19:09:00
你把那个
Connection conn = DriverManager.getConnection("Jdbc:mysql://localhost:3306/test","root","pingzheng");
把Jdbc
改成jdbc试下,具体要看你报的是什么错误,最好把错误消息粘上来看看!
3 楼
lovejavalovecpp [专家分:30] 发布于 2007-10-31 19:24:00
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.Statement.checkForDml(Statement.java:305)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:885)
at 登陆界面.LoginWindow.actionPerformed(LoginWindow.java:97)
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.pumpEventsForFilter(Unknown Source)
at java.awt.Dialog$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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)
4 楼
lovejavalovecpp [专家分:30] 发布于 2007-10-31 19:26:00
这是提示的错误 应该不是你说的那个错误
我在其他的地方都是这样写的 可以 就写记录的时候出错
6 楼
shandian534 [专家分:560] 发布于 2007-10-31 20:29:00
建议认真检查以下这条语句,包括中英文输入,以及全角半角,大小写等:
Connection conn = DriverManager.getConnection("Jdbc:mysql://localhost:3306/test","root","pingzheng");
7 楼
lovejavalovecpp [专家分:30] 发布于 2007-10-31 20:35:00
不是这个错误 import java.sql.*;
public class SimpleTest {
public static final String drivername = "com.mysql.jdbc.Driver";//定义驱动程序的名字
public static final String url = "Jdbc:mysql://localhost:3306/test";//获得数据库的URL
public static final String user = "root";
public static final String password = "pingzheng";
public static void main(String[] args) {
//设定查询语句
String queryString = "select idbook, bookname from book";
Connection conn = null;
Statement st = null;
java.sql.ResultSet rs = null;
try {
//1 加载驱动程序
Class.forName(drivername);
// 2 建立连接
conn = DriverManager.getConnection(url, user, password);
// 3 创建 Statment
st = conn.createStatement();
// 4 执行SQL语句, 获得查询结果
rs = st.executeQuery(queryString);
while(rs.next()){
System.out.println(rs.getString("idbook") + rs.getString("bookname"));
}
rs.close();
st.close();
conn.close();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
我用这个程序测试了 可以
8 楼
sjhlovejava [专家分:1690] 发布于 2007-11-01 13:34:00
stmt.executeQuery(sql);
换成
stmt.executeUpdate(sql)
你的sql语句是插入语句,而不是查询 语句
我来回复