用這個類吧.好的話,給我加加分.
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供湛河網(wǎng)站建設(shè)、湛河做網(wǎng)站、湛河網(wǎng)站設(shè)計、湛河網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、湛河企業(yè)網(wǎng)站模板建站服務(wù),十年湛河做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
import java.sql.*;
/**
* @功能: 一個JDBC的本地化API連接類,封裝了數(shù)據(jù)操作方法,只用傳一個SQL語句即可
* @作者: 李開歡
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 這里可以將常量全部放入另一個類中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println("連接中...");
try {
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功連接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println("執(zhí)行SQL語句中...");
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select")){
rs = ps.executeQuery(sql);
System.out.println("執(zhí)行完查詢操作,結(jié)果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢刪除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢更新操作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查詢結(jié)果為:");
return rs;
}
public static void closeConnection(){
System.out.println("關(guān)閉連接中...");
try {
if (rs != null) {
rs.close();
System.out.println("已關(guān)閉ResultSet");
}
if (ps != null) {
ps.close();
System.out.println("已關(guān)閉Statement");
}
if (conn != null) {
conn.close();
System.out.println("已關(guān)閉Connection");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql2 = "insert into type values(1,'教學設(shè)備')";
ConnectionDemo.getStatement(sql2);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("編號 "+"類 型");
try {
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ");
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}
首先創(chuàng)建一個連接工廠import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;public class ConnectionFactory {
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
public ConnectionFactory() {
super();
// TODO Auto-generated constructor stub
} public void OpenConn() throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://127.0.0.1:3306/guestbook";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
}catch(Exception e){
System.out.println("創(chuàng)建鏈接拋出異常為:"+e.getMessage());
}
} public ResultSet executeQuery(String sql) throws Exception{
try{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}catch(Exception e){
System.out.println("執(zhí)行查詢拋出的異常為:"+e.getMessage());
}
return rs;
} public void close() throws Exception{
try{
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println("關(guān)閉對象拋出的異常:"+e.getMessage());
}
} }
測試類 import java.sql.ResultSet;public class TestJDBC {
public static void main(String[] args) {
ConnectionFactory c= new ConnectionFactory();
try {
c.OpenConn();
String sql="select * from tb_guestbook";
ResultSet rs=c.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(2));
}
c.close();
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1、加載驅(qū)動程序。
2、創(chuàng)建連接對象。
3、創(chuàng)建sql語句執(zhí)行對象 。
4、執(zhí)行sql語句。
5、對執(zhí)行結(jié)果進行處理。
6、關(guān)閉相關(guān)的連接對象即可(順序跟聲明的順序相反)。
處理結(jié)果兩種情況:
1、執(zhí)行更新返回的是本次操作影響到的記錄數(shù)。
2、執(zhí)行查詢返回的結(jié)果是一個ResultSet對象。
ResultSet包含符合SQL語句中條件的所有行,并且它通過一套get方法提供了對這些 行中數(shù)據(jù)的訪問。
擴展資料:
Statement
要執(zhí)行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3 種類型:
1、執(zhí)行靜態(tài)SQL語句。通常通過Statement實例實現(xiàn)。
2、執(zhí)行動態(tài)SQL語句。通常通過PreparedStatement實例實現(xiàn)。
3、執(zhí)行數(shù)據(jù)庫存儲過程。通常通過CallableStatement實例實現(xiàn)。
參考資料:百度百科JAVA
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class inensshow extends JFrame {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData rsMetaData;
//GUI變量定義
private JTable table;
private JTextArea inputQuery;
private JButton submitQuery;
public inensshow()
{
//Form的標題
super( "輸入SQL語句,按查詢按鈕查看結(jié)果。" );
String url = "jdbc:mysql://localhost:3306/web";
String username = "inens";
String password = "inens";
//加載驅(qū)動程序以連接數(shù)據(jù)庫
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
connection = DriverManager.getConnection(
url, username, password );
}
//捕獲加載驅(qū)動程序異常
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"裝載 JDBC/ODBC 驅(qū)動程序失敗。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
//捕獲連接數(shù)據(jù)庫異常
catch ( SQLException sqlex ) {
System.err.println( "無法連接數(shù)據(jù)庫" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
//如果數(shù)據(jù)庫連接成功,則建立GUI
//SQL語句
String test="SELECT * FROM data";
inputQuery = new JTextArea( test, 4, 30 );
submitQuery = new JButton( "查詢" );
//Button事件
submitQuery.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
//將"輸入查詢"編輯框布置到 "CENTER"
topPanel.add( new JScrollPane( inputQuery), BorderLayout.CENTER );
//將"提交查詢"按鈕布置到 "SOUTH"
topPanel.add( submitQuery, BorderLayout.SOUTH );
table = new JTable();
Container c = getContentPane();
c.setLayout( new BorderLayout() );
//將"topPanel"編輯框布置到 "NORTH"
c.add( topPanel, BorderLayout.NORTH );
//將"table"編輯框布置到 "CENTER"
c.add( table, BorderLayout.CENTER );
getTable();
setSize( 500, 300 );
//顯示Form
show();
}
private void getTable()
{
try {
//執(zhí)行SQL語句
String query = inputQuery.getText();
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
//在表格中顯示查詢結(jié)果
displayResultSet( resultSet );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void displayResultSet( ResultSet rs )
throws SQLException
{
//定位到達第一條記錄
boolean moreRecords = rs.next();
//如果沒有記錄,則提示一條消息
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"結(jié)果集中無記錄" );
setTitle( "無記錄顯示" );
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
//獲取字段的名稱
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i = rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
//獲取記錄集
do {
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
//在表格中顯示查詢結(jié)果
table = new JTable( rows, columnHeads );
JScrollPane scroller = new JScrollPane( table );
Container c = getContentPane();
c.remove(1);
c.add( scroller, BorderLayout.CENTER );
//刷新Table
c.validate();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{
Vector currentRow = new Vector();
for ( int i = 1; i = rsmd.getColumnCount(); ++i )
currentRow.addElement( rs.getString( i ) );
//返回一條記錄
return currentRow;
}
public void shutDown()
{
try {
//斷開數(shù)據(jù)庫連接
connection.close();
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to disconnect" );
sqlex.printStackTrace();
}
}
public static void main( String args[] )
{
final inensshow app =
new inensshow();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
app.shutDown();
System.exit( 0 );
}
}
);
}
}
------------------------------------------------------------
這次在WIN98中就不好使了。因為Mysql的驅(qū)動程序沒有也沒能加入到CLASSPATH 當中,但是JSP卻可以使用(JSP的98驅(qū)動加載詳見Jsp與Mysql連接查錯文章),所以這次我是在XPServer中測試的。
網(wǎng)頁題目:鏈接數(shù)據(jù)的代碼java 連接數(shù)據(jù)庫代碼java
轉(zhuǎn)載源于:http://vcdvsql.cn/article24/ddijdce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站策劃、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)