JAV连SQL~(100分)

  • 主题发起人 主题发起人 zjhui
  • 开始时间 开始时间
Z

zjhui

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我连接SQL 2000的代码,驱动没错,我DatabaseName明明指定的是QQServer为什么我用getCatalog(),返回的都是master下面是我的代码,哪里错了,我实在是找不错,请快点帮忙
代码:
下,
import java.sql.*;
import java.io.*;
public class JDBCTest {
private String dbURL;
private String user;
private String password;
private static Statement sta = null;
public static void main(String[] args) {
try{
JDBCTest bridge = new JDBCTest();
bridge.setURL("jdbc:microsoft:sqlserver://192.168.0.1:1433;DatabaseName = QQServer");
bridge.setUser("sa");
bridge.setPassword("sa");
Connection con = bridge.getConnection();
System.out.println("~~QQQQQQQQQQQQQQQQQQQQQQqq");
System.out.println(con.getCatalog()+"~~~~~~~~~~~~~~~");
try{
sta = con.createStatement();
}catch(Exception e){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}

/** String sqlStr = "SELECT * FROM ";
ResultSet rs;
rs = sta.executeQuery(sqlStr);*/
// System.out.println(rs.getString(1));
con.close();
}catch(Exception e){
e.printStackTrace();
System.out.println("创建错误");
}
}
public Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
return DriverManager.getConnection(dbURL,user,password);
}catch(Exception e){
e.printStackTrace();
System.out.println("加载驱动错误~~~~~~~~~~~~~~~~");
}
return null;
}
public void setURL(String dbURL){
this.dbURL = dbURL;
}
public void setUser(String user){
this.user = user;
}
public void setPassword(String password){
this.password = password;
}
}
 
老大~~~
没答案的话就不用顶了[:(!]
 
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class jdbc //定义主类
{
public static void main(String args[])
{
GUI gui=new GUI();
//创建类GUI的对象 
gui.pack();
//装载执行GUI类
}
}
class GUI extends Frame implements Action Listener
{
TextArea text;
Panel panel;
TextField sno;
Button btn;
GUI() //构造方法 
{
super("物资情况查询");
setLayout(new BorderLayout());
setBackground(Color.cyan);
setVisible(true);
text=new TextArea();
btn=new Button("查询");
sno=new TextField(16);
panel=new Panel();
panel.add(new Label("输入被查询的物资编号:"));
panel.add(sno);
panel.add(btn);
add("North",panel);
add(text,"Center");
text.setEditable(false);
btn.addActionListener(this);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn) //当用户按下查询按钮时
{
text.setText("查询结果"+"");//显示提示信息
try
{
liststudent();
}
catch (SQLException ee) { }

}
}
public void Liststudent() throws SQLException //针对数据库的操作
{
String bh,mc,xh,lb,dw,sj;
int sl;
float dj,je;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e) { }
Connection con=DriverManager.getConnection("jdbc:odbc:wzgl");
Statement sql=con.createStatement();
//创建Statement对象 
ResultSet rs=sql.executeQuery("select * from wuzi");
while(rs.next()) //输出被查询的情况 
{
bh=rs.getString("物资编号");
mc=rs.getString("物资名称");
h=rs.getString("规格型号");
lb=rs.getString("类别");
dw=rs.getString("计量单位");
sl=rs.getInt("数量");
dj=rs.getFloat("单价");
je=rs.getFloat("金额");
sj=rs.getDate("时间").toString();
if(bh.trim().equals(sno.getText().trim()))
{
text.append("+"物资编号"+"
"+"物资名称"+"
"+"规格型号"+"
"+"类别"+"
"+"计量单位"+"
"+"数量"+"
"+"单价"+"
"+"金额"+"
"+"时间"+");
text.append("+bh+"
"+mc+"
"+xh+"
"+lb+"
"+dw+"
"+sl+"
"+dj+"
"+je+"
"+sj+"
"+");
}
}
}
}
 

Similar threads

I
回复
0
查看
629
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
1K
import
I
I
回复
0
查看
626
import
I
后退
顶部