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");
}
}
}
数据连接部分通过测试源代码-----java学习笔记之4
原创
©著作权归作者所有:来自51CTO博客作者superdont的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
4Java学习笔记之数据结构——队列
队列也是一种特殊的线性表,像栈是先进后出(FILO),而队列就像排队一样,先进的先出(FI
数据结构 队列 中间件 List 消息队列 -
大数据测试学习笔记之01
究的方向的笔记:大数据测试整个学习笔记以短文为主,记录一些关键信息和思考预计每周一篇短文进行记录,可...
软件测试 大数据 人工智能 编程语言 机器学习 -
大数据测试学习笔记之基准测试HiBench
试整个学习笔记以短文为主,记录一些关键信息和思考预计每周一篇短文进行记录,可能是理论、概念、技术、工...
算法 大数据 python 机器学习 人工智能