Topic: 是高手就来看看! |
Print this page |
1.是高手就来看看! | Copy to clipboard |
Posted by: hujianso Posted on: 2004-09-08 10:56 大家好,我是一个刚刚学java不久的新人。由于种种需要,我想用java的jdbc调用sqlserver中的数据库,并以表格的形式显示出来。但是由于资源的有限,所以带着一份憧憬的心态来到了网上,来到的你们的论坛,这里有很多的关于jdbc的知识,象jdbc讲的都很好,很有参考价值。但是我总感觉好象缺了点什么,后来才知道:我看完后,什么也还是不会做,或者是自己太笨了吧,没有什么悟性。但不管怎么样,我能不能提一个小小的建议,就是:能不能相应的给Appet,Japplet,Frame,Jframe,和application都给一个完整代码的具体的例子呢?比如说sql server 2000 中见了一个表,然后用java(非jsp,javabean,而是一个java文件)通过odbc在ie或appletviewer中象数据库一样以表格的形式表示出来!?谢谢了哦 :( |
2.Re:是高手就来看看! [Re: hujianso] | Copy to clipboard |
Posted by: java8110 Posted on: 2004-09-08 11:23 jbuilder好像有这种例子 |
3.Re:是高手就来看看! [Re: hujianso] | Copy to clipboard |
Posted by: hujianso Posted on: 2004-09-08 13:28 hehe,一开始我也想直接要jbuilder来实现的。但是越到后面我越发现。对于我们初学者来说,一开始用jbuilder并不好,因为里面的代码太冗长了,有时候很简单的add()的一个东西代码都那么多。所以我想还是自己纯手写的方式来写好。还好,我现在可以实现上面的了。不过还是要谢谢你! |
4.自己多实践,下面的希望对你有帮助!(一个完整的程序,连接数据库,操作数据库) [Re: hujianso] | Copy to clipboard |
Posted by: msl_sy Posted on: 2004-09-08 23:17 */ package study; /** * @author Administrator * * 更改所生成类型注释的模板为 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; //import MyWindowListener.*; class Testjdbc extends Frame implements ActionListener,KeyListener,WindowListener,MouseListener { Color c ; //= new Color(140,148,240); Button bok = new Button("打开"); Button bcancel = new Button("关闭"); Button bconnect = new Button("连接数据库"); TextArea msg = new TextArea("请在此输入留言文字!"); Testjdbc() { // CheckboxGroup cg = new CheckboxGroup(); Checkbox cb1 = new Checkbox("中文",true,cg); Checkbox cb2 = new Checkbox("英文",false ,cg); cb2.setState(true); cb1.setState(true); //下拉选择窗口 Choice ch = new Choice(); ch.add("东北"); ch.add("西北"); ch.add("中国"); ch.add("美国"); //文本编辑区域 FlowLayout border = new FlowLayout(FlowLayout.LEFT); this.setLayout(border); this.setLayout(null); //增加三个按钮 this.add(bok); this.add(bcancel); this.add(bconnect); //调整位置和大小 bok.setSize(70,30); bok.setLocation(100,400); bconnect.setSize(70,30); bconnect.setLocation(200,400); bcancel.setSize(70,30); bcancel.setLocation(300,400); //增加下拉选项 this.add(ch); this.setTitle(ch.getItem(1)); ch.setSize(80,40); ch.setLocation(400,400); //增加文本编辑 c = new Color(111,149,150); this.add(msg); msg.setSize(200,150); msg.setLocation(10,30); msg.setEnabled(true); msg.setBackground(c); //增加复选按钮 // this.add(cg); // cg.setSize(200,200); // cg.setLocation(300,300); this.add(cb1); this.add(cb2); //add one textarea this.add(msg); // public void paint(Graphics g) // { // g.setColor(Color.red); // g.drawOval(80,80,120,120); // } /////////////////////////////////// this.addWindowListener(this); bok.addKeyListener(this); bcancel.addMouseListener(this); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bconnect.addActionListener(this); bok.addActionListener(this); bcancel.addActionListener(this); } /////////////////////////////////////// public void actionPerformed(ActionEvent e) { if (e.getSource() == bconnect) { // bok.setLabel("查找被按下!"); // JOptionPane.showMessageDialog(null,"查找被按下!"); // JOptionPane.showMessageDialog(null,"aaaa"); // JOptionPane.showConfirmDialog(null,"确定","yes or no",JOptionPane.YES_NO_OPTION); ///////////////////////////////// String str1 = ""; try{ //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); }catch (ClassNotFoundException e1){System.out.print("SQLException" + e1.getMessage());} try{ String user="sa"; String password=""; Connection con1 = DriverManager.getConnection("jdbc:odbc:test"); Statement st1 = con1.createStatement(); ResultSet rs1 = st1.executeQuery("select * from t_user"); while(rs1.next()){ str1 = str1 + rs1.getString("user_name")+"\t"+rs1.getString("user_id")+"\t"+rs1.getLong(3)+"\n"; //rs1.getLong(5) + "\t"+ rs1.getLong(6)+"\t"+ //rs1.getLong(7)+"\n"; } st1.close(); con1.close(); msg.setText(str1); JOptionPane.showMessageDialog(null,str1); }catch (SQLException e2){System.out.println("SQLException" + e2.getMessage()); // JOptionPane.showMessageDialog(null,"表没有找到!"); } /////////////////////////////////////// } if (e.getSource() == bok) { JOptionPane.showMessageDialog(null,"保存按钮被按下!"); anotherFrame jf = new anotherFrame(); jf.setVisible(true); jf.setSize(400,500); } if(e.getSource() == bcancel) { System.exit(0); } } /////////////////////////////////////// public void keyPressed(KeyEvent e){} public void keyReleased(KeyEvent e) { bcancel.setLabel("love you"); //f.ShowMessage("key char =" + e.getKeyChar()); //f.ShowMessage("key code =" + e.getKeyCode()); // f.ShowMessage("key modifier =" + e.getModifiers()); } public void keyTyped(KeyEvent e){} ////////////////////////////////////// public void windowActivated(WindowEvent e){} public void windowDeactivated(WindowEvent e){} public void windowOpened(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowClosing(WindowEvent e){System.exit(1);} public void windowIconified(WindowEvent e){} public void windowDeiconified(WindowEvent e){} ////////////////////////////////////// public void mousePressed(MouseEvent e){} public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e) { // TestFrame f = new TestFrame(); // f.setSize(300,400); // f.setVisible(true); // JButton j1 = new JButton("new"); // j1.setSize(80,80); // j1.setLocation(80,60); // j1.setText("odbc"); // Container c = f.getContentPane(); // c.add(j1); } ////////////////////////////////////// public static void main(String[] args) { Testjdbc f = new Testjdbc(); f.setTitle("练习窗口"); f.setVisible(true); f.setLocation(100,100); f.setSize(500,500); f.c = new Color(150,150,190); f.setBackground(f.c); } } |
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 |