JAVA链接数据库的类 详见我的博客http://blog.csdn.net/zhaotao_king/archive/2008/12/18/3549889.aspx 可以根据下面的这种方法从数据库中取得你想要的值,并形成表格 public String[][] sql(String str){//从数据库中取出要返回的值 try{ CachedRowSetImpl crs=new CachedRowSetImpl(); crs=db.dbquery("select * from ziliao where bmmc='"+str+"'"); //System.out.println("select * from ziliao where bmmc='"+str+"'"); int hang=crs.size(); String data=new String[hang][7]; int i=0; while(crs.next()){ data[i][0]=new String(((crs.getString("ygbh")).trim()).getBytes("iso-8859-1"),"GBK"); data[i][1]=new String(((crs.getString("ygxm")).trim()).getBytes("iso-8859-1"),"GBK"); data[i][2]=new String(((crs.getString("ygxb")).trim()).getBytes("iso-8859-1"),"GBK"); data[i][3]=(crs.getDate("ygsr")+"").trim(); data[i][4]=new String(((crs.getString("ygxl")).trim()).getBytes("iso-8859-1"),"GBK"); data[i][5]=new String(((crs.getString("ygdh")).trim()).getBytes("iso-8859-1"),"GBK"); data[i][6]=new String(((crs.getString("ygdz")).trim()).getBytes("iso-8859-1"),"GBK"); i++; if(i>=hang)break; } }catch(Exception e){ System.out.println(e.toString()); } return data; }
希望能对你有所帮助
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.sql.*; public class 用户登录框 extends JFrame{ public void run() { final JTextField t; final JPasswordField t1; this.setTitle("测试"); JLabel l = new JLabel("用户名:"); JLabel l1 = new JLabel("密码:"); t = new JTextField(20); t1 = new JPasswordField(20); t1.setEchoChar('●'); JPanel p = new JPanel(); p.setLayout(new GridLayout(2,1)); p.add(l);p.add(t); p.add(l1);p.add(t1); this.add(p); JPanel p1 = new JPanel(); final JButton b = new JButton("确认"); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String x = t.getText(); char[] y = t1.getPassword(); String z = y.toString(); run1(x,z); if(x.length()> 0 && x.length() <= 15){ if(y.length > 0 && y.length <= 15){ JOptionPane.showMessageDialog(b,x+"您是注册用户"+"\n"+"欢迎使用小树流氓软件"); }else JOptionPane.showMessageDialog(b,"用户名或密码不能为空或者大于15位"); }else JOptionPane.showMessageDialog(b,"用户名或密码不能为空或者大于15位");
} }); final JButton b1 = new JButton("取消"); b1.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ System.exit(0); } }); p1.add(b);p1.add(b1); this.add(p1,BorderLayout.SOUTH); pack(); this.setVisible(true); } public void run1(String x,String y){ Connection con = null; Statement stmt = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433; DatabaseName=student", "javaliu", "lgs1162"); System.out.println("数据库连接成功!"); stmt = con.createStatement(); stmt.executeUpdate("INSERT INTO buyer VALUES('"+x+"','"+y+"')"); ResultSet rs = stmt.executeQuery("select * from buyer"); while (rs.next()) { System.out. println("名字:"+rs.getString("y_name")); System.out.println("密码:"+rs.getString("y_password")); System.out.println("****************");
可以根据下面的这种方法从数据库中取得你想要的值,并形成表格 public String[][] sql(String str){//从数据库中取出要返回的值
try{
CachedRowSetImpl crs=new CachedRowSetImpl();
crs=db.dbquery("select * from ziliao where bmmc='"+str+"'");
//System.out.println("select * from ziliao where bmmc='"+str+"'");
int hang=crs.size();
String data=new String[hang][7];
int i=0;
while(crs.next()){
data[i][0]=new String(((crs.getString("ygbh")).trim()).getBytes("iso-8859-1"),"GBK");
data[i][1]=new String(((crs.getString("ygxm")).trim()).getBytes("iso-8859-1"),"GBK");
data[i][2]=new String(((crs.getString("ygxb")).trim()).getBytes("iso-8859-1"),"GBK");
data[i][3]=(crs.getDate("ygsr")+"").trim();
data[i][4]=new String(((crs.getString("ygxl")).trim()).getBytes("iso-8859-1"),"GBK");
data[i][5]=new String(((crs.getString("ygdh")).trim()).getBytes("iso-8859-1"),"GBK");
data[i][6]=new String(((crs.getString("ygdz")).trim()).getBytes("iso-8859-1"),"GBK");
i++;
if(i>=hang)break;
}
}catch(Exception e){
System.out.println(e.toString());
}
return data;
}
希望能对你有所帮助
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
public class 用户登录框 extends JFrame{
public void run() {
final JTextField t;
final JPasswordField t1;
this.setTitle("测试");
JLabel l = new JLabel("用户名:");
JLabel l1 = new JLabel("密码:");
t = new JTextField(20);
t1 = new JPasswordField(20);
t1.setEchoChar('●');
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(l);p.add(t);
p.add(l1);p.add(t1);
this.add(p);
JPanel p1 = new JPanel();
final JButton b = new JButton("确认");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String x = t.getText();
char[] y = t1.getPassword();
String z = y.toString();
run1(x,z);
if(x.length()> 0 && x.length() <= 15){
if(y.length > 0 && y.length <= 15){
JOptionPane.showMessageDialog(b,x+"您是注册用户"+"\n"+"欢迎使用小树流氓软件");
}else
JOptionPane.showMessageDialog(b,"用户名或密码不能为空或者大于15位");
}else
JOptionPane.showMessageDialog(b,"用户名或密码不能为空或者大于15位");
}
});
final JButton b1 = new JButton("取消");
b1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
System.exit(0);
}
});
p1.add(b);p1.add(b1);
this.add(p1,BorderLayout.SOUTH);
pack();
this.setVisible(true);
}
public void run1(String x,String y){
Connection con = null;
Statement stmt = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433; DatabaseName=student", "javaliu",
"lgs1162");
System.out.println("数据库连接成功!");
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO buyer VALUES('"+x+"','"+y+"')");
ResultSet rs = stmt.executeQuery("select * from buyer");
while (rs.next()) {
System.out.
println("名字:"+rs.getString("y_name"));
System.out.println("密码:"+rs.getString("y_password"));
System.out.println("****************");
}
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new 用户登录框().run();
}
}