package
database;
import
java.sql.
*
;
import
java.io.
*
;
public

class
DBColumn
...
{

public static void main(String[] args) ...{
Connection cnotallow=null;
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try
...{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("驱动程序已加载");
//SQL SERVER的登陆方式必须为使用SQL SERVER密码登陆认证方式
cnotallow=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dangwei","sa","as");//SERVERNAME:1433","sa","as");
//con.setCatalog("GoodsSupply");
System.out.println("OK,成功连接到数据库");
}catch(Exception ex) ...{
ex.printStackTrace();
}
try
...{
sm=con.createStatement();
System.out.println("输入表名");
tableName=input.readLine();
while(true) ...{
System.out.println("输入列名(为空时程序结束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command);
if(!rs.next())
System.out.println("表名或列名输入有误");
else ...{
System.out.println("查询结果为:");
do
...{
result=rs.getString(cName);
//result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(Exception ex) ...{
ex.printStackTrace();
}
}
}







package database;

import
java.sql.
*
;

public

class
DBTools
...
{



public static void main(String[] args) ...{
Connection conn;
try ...{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=dangwei", "sa", "as");
System.out.println("连接成功!");
}
catch (Exception e) ...{
e.printStackTrace();
}
}
}








package database;
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
ActionListener

...
{

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) ...{ System.out.println("error1"); ee.printStackTrace();}

}

}

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 cnotallow=DriverManager.getConnection("jdbc:odbc:lilizong","sa","as");

Statement sql=con.createStatement(); //创建Statement对象

ResultSet rs=sql.executeQuery("select * from geren");

while(rs.next()) //输出被查询的情况

...{

bh=rs.getString("name");

mc=rs.getString("sex");

//xh=rs.getString("birthday");

//lb=rs.getString("city");

//dw=rs.getString("nation");

//sl=rs.getInt("cjdate");

//dj=rs.getFloat("rddate");

//je=rs.getFloat("degree");

//sj=rs.getDate("xueli").toString();

if(bh.trim().equals(sno.getText().trim()))

...{

text.append(' '+"物资编号"+" "+"物资名称"+" "+"规格型号"+" "+"类别"+" "+"计量单位"+" "+"数量"+" "+"单价"+" "+"金额"+" "+"时间"+' ');

//text.append(' '+bh+" "+mc+" "+xh+" "+lb+" "+dw+" "+sl+" "+dj+" "+je+" "+sj+" "+' ');
text.append(' '+bh+" "+mc+" "+" ");

}

}

}

}






package database;

import
java.sql.
*
;

public

class
lookup
...
{
public static void main(String[] args)
throws SQLException, ClassNotFoundException ...{
//定义了数据库连接串
String dbUrl = "jdbc:odbc:lilizong";
//数据库的用户名
String user = "sa";
//数据库的用户口令
String password = "as";
// 加载jdbc-odbc bridge驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 与url指定的数据源建立连接
Connection c = DriverManager.getConnection(dbUrl, user, password);
//采用Statement进行查询
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT * from geren");
while(r.next()) ...{
// 打印字段信息
//System.out.println(r.getString("sex") + ", " + r.getString("name ") );
//System.out.println("lilziong");
System.out.println(r.getString("name"));
}
// 关闭Statement,其上的ResultSet也将关闭
s.close();
}
}






package database;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.sql.
*
;

public

class
ODBCBridge
...
{

public static void main(String[] args) ...{
String url="jdbc:odbc:lilizong";
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dangwei";
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try ...{
try ...{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动
}catch(ClassNotFoundException e)...{
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection cnotallow=DriverManager.getConnection(url,"sa","as"); //使用SQL-SERVER2000认证
DatabaseMetaData dmd=con.getMetaData(); //DMD为连接的相应情况
System.out.println("连接的数据库:"+dmd.getURL());
System.out.println("驱动程序:"+dmd.getDriverName());
sm=con.createStatement();
System.out.println("输入表名");
tableName=input.readLine();
while(true) ...{
System.out.println("输入列名(为空时程序结束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command); //执行查询
if(!rs.next())
System.out.println("表名或列名输入有误");
else ...{
System.out.println("查询结果为:");
do
...{
result=rs.getString(cName);
// 数据库语言设置为中文,不用转换编码
// result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(SQLException ex) ...{
System.out.println("SQLException:");
while(ex!=null) ...{
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) ...{
System.out.println("IOException");
}
}
}





package database;

import
java.sql.
*
;
import
java.util.
*
;
public

class
test
...
{
public static void main(String args[])...{

try
...{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Properties prop = new Properties();
prop.put("user", "sa");
prop.put("password","as");
prop.put("charSet","gb2312");

//Connection cnotallow=DriverManager.getConnection("jdbc:odbc:chinese",prop);
Connection cnotallow=DriverManager.getConnection("jdbc:odbc:lilizong",prop);
Statement stmt=conn.createStatement();
//stmt.execute(args[0]);
stmt.execute("select * from geren");
System.out.println("sucess");
}

catch(ClassNotFoundException e)
...{
// JOptionPane.showMessageDialog(null,"Unable To Load The Driver Class","Login Message", JOptionPane.ERROR_MESSAGE);

}
catch(SQLException e)
...{
e.printStackTrace();
System.out.println(e.getErrorCode());
}

}
}







package database;

//
导入Java SQL包,连接数据库必需;

import
java.sql.
*
;

public

class
TestDB
...
{
public static void main(String[] args) ...{
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dangwei";
//默认断口为1433
//数据库名为PUBS
String userName = "sa"; //SQL用户名
String userPwd = "as"; //密码
Connection dbConn;

try ...{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
}
catch(Exception e) ...{
e.printStackTrace();
System.out.println("lilizong");
}
}
}





package database;

import
java.sql.
*
;
import
java.util.
*
;

class
testDBdriver
...
{
public static void main(String[] arge)...{
try...{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Enumeration e=DriverManager.getDrivers();
for(;e.hasMoreElements();)
//System.out.println(e.nextElement());
System.out.println("li");
}catch(java.lang.Exception e)...{
System.out.println("Code has error");
}
}
}