关于java 数据库编程

damonZengg 2008-06-12 09:02:55

public void fuck() {
Connection con;
Statement sta;
ResultSet rs;
String sql_driver;
String sql_url;
sql_driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
sql_url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=restaurant";
String user = "sa";
String pwd = "ericulje";
try {

Class.forName(sql_driver);
System.out.println("driver is ok");
con = DriverManager.getConnection(sql_url, user, pwd);
System.out.println("connection is ok");
sta = con.createStatement();
rs = sta.executeQuery("select fangtaiId from fangtaiInfo");
if (rs.next()) {
String str1, str2, str3, str4, str5, str6, str7, str8, str9,
str10;
str1 = rs.getString(1);
str2 = rs.getString(2);
str3 = rs.getString(3);
str4 = rs.getString(4);
str5 = rs.getString(5);
str6 = rs.getString(6);
str7 = rs.getString(7);
str8 = rs.getString(8);
str9 = rs.getString(9);
str10 = rs.getString(10);
String table[] = {"str1", "str2", "str3", "str4", "str5",
"str6", "str7", "str8", "str9", "str10"};
jComboBox1 = new JComboBox(table);
}
con.close();
} catch (Exception e) {
e.printStackTrace(); //printStackTrace();
}
}


我想要实现的功能:从数据库的表里取出一列数据,再把这列数据加入下拉菜单(JComboBox)中。
...全文
56 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
damonZengg 2008-06-12
  • 打赏
  • 举报
回复

package rams;

import java.awt.*;

import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Dimension;
import java.sql.*;
import java.util.Vector;



/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class BookService_Frame extends JFrame {
JPanel contentPane;

XYLayout xYLayout1 = new XYLayout();
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
XYLayout xYLayout3 = new XYLayout();
XYLayout xYLayout4 = new XYLayout();
JTextField jTextField1 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox1 = new JComboBox();
Connection con;
JLabel jLabel3 = new JLabel();
JComboBox jComboBox2 = new JComboBox();
JLabel jLabel4 = new JLabel();
JTextField jTextField2 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel5 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
public BookService_Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(400, 300));
setTitle("预定服务");
this.setVisible(true);
jPanel1.setLayout(xYLayout2);
jPanel1.setNextFocusableComponent(jPanel2);
jPanel1.setToolTipText("");
jTabbedPane1.setToolTipText("");
jPanel2.setLayout(xYLayout3);
jPanel3.setLayout(xYLayout4);
jLabel1.setText("订单号:");
jLabel2.setText("房台号:");
jComboBox1.setInputVerifier(null);
//String table[]={"Z01","Z02","Z03","Z04","Z05","Z06","Z07","Z08","Z09","Z10"};
//jComboBox1 = new JComboBox(table);
jLabel3.setText("服务员:");
jLabel4.setText("结账金额:");
jButton1.setText("提交");

jButton2.setText("退出");
jLabel5.setText("请输入您要删除订单的订单号:");
jButton3.setText("确定");
jButton4.setText("退出");
contentPane.add(jTabbedPane1, new XYConstraints(2, 7, 398, 305));
jTabbedPane1.add(jPanel1, "增加订单");
jTabbedPane1.add(jPanel2, "已有订单");
jTabbedPane1.add(jPanel3, "取消订单");
jPanel1.add(jLabel1, new XYConstraints(40, 21, 60, 21));
jPanel1.add(jTextField1, new XYConstraints(103, 21, 73, -1));
jPanel1.add(jLabel3, new XYConstraints(204, 23, -1, -1));
jPanel1.add(jComboBox2, new XYConstraints(270, 20, 73, -1));
jPanel1.add(jLabel2, new XYConstraints(204, 102, -1, -1));
jPanel1.add(jComboBox1, new XYConstraints(269, 92, 73, 25));
jPanel1.add(jButton2, new XYConstraints(256, 185, -1, -1));
jPanel1.add(jButton1, new XYConstraints(119, 185, -1, -1));
jPanel1.add(jLabel4, new XYConstraints(40, 96, 62, 21));
jPanel1.add(jTextField2, new XYConstraints(104, 95, 73, 22));
jPanel3.add(jLabel5, new XYConstraints(17, 50, 190, 36));
jPanel3.add(jTextField3, new XYConstraints(208, 57, 69, 25));
jPanel3.add(jButton3, new XYConstraints(92, 183, -1, -1));
jPanel3.add(jButton4, new XYConstraints(233, 182, -1, -1));
jTabbedPane1.setSelectedComponent(jPanel1);
fuck();
}

public void fuck() {
Connection con;
Statement sta;
ResultSet rs;
String sql_driver;
String sql_url;
sql_driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
sql_url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=restaurant";
String user = "sa";
String pwd = "ericulje";
try {

Class.forName(sql_driver);
System.out.println("driver is ok");
con = DriverManager.getConnection(sql_url, user, pwd);
System.out.println("connection is ok");
sta = con.createStatement();
rs = sta.executeQuery("select fangtaiId from fangtaiInfo");
Vector<String> vec = new Vector<String>();

while (rs.next()) {

String strfangtaiId = rs.getString("fangtaiId");

vec.addElement(strfangtaiId);
}

jComboBox1 = new JComboBox(vec);

con.close();
} catch (Exception e) {
e.printStackTrace(); //printStackTrace();
}
}
}

把整个代码放上来吧,我感觉我的funtion调用除了问题
damonZengg 2008-06-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liangCK 的回复:]
Java codeSystem.out.println("driver is ok");
con = DriverManager.getConnection(sql_url, user, pwd);
System.out.println("connection is ok");
sta = con.createStatement();
rs = sta.executeQuery("select fangtaiId from fangtaiInfo");

Vector<String> vec=new Vector<String>();

while (rs.next()) {
String strfangtaiId=rs.getString("fangtaiId");

vec.addElement(strfangtaiId);
}

jComb…
[/Quote]
我用你这个代码放进去还是不行。
liangCK 2008-06-12
  • 打赏
  • 举报
回复
依依..我没错吧.
liangCK 2008-06-12
  • 打赏
  • 举报
回复
3、只关闭了数据库连接 , Statement以及ResultSet均没有关闭
----------
呵呵..估计是想gc...垃圾回收吧..
yiyi_wx 2008-06-12
  • 打赏
  • 举报
回复
1、if改为while 这需要循环读取
用if只是读到下一条 并没有循环
2、你rs取数据不对吧。。。sql语句"select fangtaiId from fangtaiInfo"
只取了一列数据 怎么用了rs.getString(1);....rs.getString(10);取了10列?
3、只关闭了数据库连接 , Statement以及ResultSet均没有关闭
liangCK 2008-06-12
  • 打赏
  • 举报
回复
System.out.println("driver is ok");
con = DriverManager.getConnection(sql_url, user, pwd);
System.out.println("connection is ok");
sta = con.createStatement();
rs = sta.executeQuery("select fangtaiId from fangtaiInfo");

Vector<String> vec=new Vector<String>();

while (rs.next()) {
String strfangtaiId=rs.getString("fangtaiId");

vec.addElement(strfangtaiId);
}

jComboBox1 = new JComboBox(vec);


//差不多是这样子吧..如果没记错的话..
liangCK 2008-06-12
  • 打赏
  • 举报
回复
取一列?..
..你这是取一行啊...

.取一列的..你用循环啊..
liangCK 2008-06-12
  • 打赏
  • 举报
回复
你的函数名..是否可以改改..晕..
damonZengg 2008-06-12
  • 打赏
  • 举报
回复
上面代码编译没错,但运行程序后,JComboBox里并没有我要的数据

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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