資料連線部分通過測試原始碼-----java學習筆記之4
一下程式碼全部來自網際網路,經過本人整理修改,全部可用。
package database;
import java.sql.*;
import java.io.*;
public class DBColumn {
public static void main(String[] args) {
Connection con=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密碼登陸認證方式
con=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();
}
}
}
import java.sql.*;
import java.io.*;
public class DBColumn {
public static void main(String[] args) {
Connection con=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密碼登陸認證方式
con=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();
}
}
}
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 con=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+" "+" ");
}
}
}
}
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 con=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();
}
}
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 con=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");
}
}
}
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 con=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 conn=DriverManager.getConnection("jdbc:odbc:chinese",prop);
Connection conn=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());
}
}
}
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 conn=DriverManager.getConnection("jdbc:odbc:chinese",prop);
Connection conn=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");
}
}
}
//匯入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");
}
}
}
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學習筆記:資料結構之線性表(雙向連結串列)Java筆記資料結構
- 【部分】Java速成學習筆記Java筆記
- 004 通過連結串列學Rust之測試Rust
- Java學習筆記4Java筆記
- java.security.Provider 原始碼學習筆記JavaIDE原始碼筆記
- 001 通過連結串列學習Rust筆記之前言Rust筆記
- 【JDBC】java連線池模擬測試連線Oracle資料庫指令碼參考JDBCJavaOracle資料庫指令碼
- UE4(5)逆向學習筆記(三)——UEDumper原始碼學習筆記原始碼
- Dubbo原始碼學習之-通過原始碼看看dubbo對netty的使用原始碼Netty
- Retrofit原始碼學習筆記原始碼筆記
- 大資料之 Hadoop學習筆記大資料Hadoop筆記
- Java的JDBC通過SSH Tunnel連線MySQL資料庫JavaJDBCMySql資料庫
- 通過觸發器記錄資料庫連線資訊觸發器資料庫
- 深圳軟體測試學習:【資料庫】-【oracle】-連線查詢資料庫Oracle
- substrate學習筆記13:連線parachain筆記AI
- 《Android原始碼設計模式》學習筆記之ImageLoaderAndroid原始碼設計模式筆記
- Java集合原始碼學習(4)HashSetJava原始碼
- 【軟體測試】學習筆記筆記
- 學習筆記 sync/RWMutex原始碼筆記Mutex原始碼
- jQuery原始碼學習筆記一jQuery原始碼筆記
- vue原始碼學習筆記1Vue原始碼筆記
- Adnroid原始碼學習筆記:Handler 執行緒間通訊原始碼筆記執行緒
- 雲端計算學習路線原始碼框架筆記:Mysql原始碼一原始碼框架筆記MySql
- 雲端計算學習路線原始碼框架筆記:Mysql原始碼二原始碼框架筆記MySql
- 雲端計算學習路線原始碼框架筆記:Mysql原始碼三原始碼框架筆記MySql
- swoft 學習筆記之資料庫操作筆記資料庫
- 小白學習大資料測試之hadoop大資料Hadoop
- 學習筆記之IdentityServer4(一)筆記IDEServer
- 記學習滲透測試之破解密碼一解密密碼
- 記學習滲透測試之破解密碼二解密密碼
- Java學習筆記之----------Java基本知識Java筆記
- C++學習筆記,知識點+程式碼測試C++筆記
- 009 通過連結串列學習Rust之IntoIterRust
- 010 通過連結串列學習Rust之IterRust
- 011 通過連結串列學習Rust之IterMutRust
- 通過原始碼學習@functools.lru_cache原始碼
- Java學習筆記8-資料型別Java筆記資料型別