LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3231|回复: 6

怎样用java连mysql?

[复制链接]
发表于 2004-6-16 12:39:37 | 显示全部楼层 |阅读模式
举个例子吧
发表于 2004-6-16 14:16:06 | 显示全部楼层
[PHP]
package firstexample;

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

public class FirstExample {
  public static void main (String args []) {
    Connection myConnection = null;
    Statement myStatement = null;
    try {
      try {
        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").
            newInstance();
      }
      catch (ClassNotFoundException ex) {
      }
      catch (IllegalAccessException ex) {
      }
      catch (InstantiationException ex) {
      }
//      DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
      // create a Connection object, and connect to the database
//      jdbc:microsoft:sqlserver://localhost:2040
      myConnection = DriverManager.getConnection(
          "jdbc:microsoft:sqlserver://localhost:1433;User=saassword=;DatabaseName=XinWeiDB"
//          ,"FreeDove","FreeDove"
          );
      // create a Statement object
      myStatement = myConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
      // create a ResultSet object, and populate it with the
      // result of a SELECT statement that retrieves the
      // user and sysdate variables from the database via
      // the dual table - the executeQuery() method of the
      // Statement object is used to perform the SELECT
      ResultSet myResultSet = myStatement.executeQuery(
          "SELECT * FROM test");
      // retrieve the row from the ResultSet using the
      System.out.println("OK.3");

      // retrieve the user from the row in the ResultSet using the
      myResultSet.getMetaData();
      Vector v = new Vector();
        myResultSet.absolute(14);
        System.out.println("rel:" + myResultSet.getInt(1));

      myResultSet.next();
      // retrieve the sysdate from the row in the ResultSet using
//      Timestamp currentDateTime = myResultSet.getTimestamp("sysdate");
//      System.out.println("hi " + user +
//        ", the current date and time is " + currentDateTime);
      // close this ResultSet object using the close() method
      myResultSet.afterLast();
      myResultSet.previous();
      myResultSet.close();
    }catch (SQLException e){
      System.out.println("Error code = " + e.getErrorCode());
      System.out.println("Error message = " + e.getMessage());
    }finally{
      try {
        // close the Statement object using the close() method
        if (myStatement != null) {
          myStatement.close();
        }
        // close the Connection object using the close() method
        if (myConnection != null) {
          myConnection.close();
        }
      } catch (SQLException e) {
        System.out.println("Error code = " + e.getErrorCode());
        System.out.println("Error message = " + e.getMessage());
      }
    }
  } // end of main()
}

[/PHP]

这是找来的连接sql2000的例子,你把驱动变成mysql的就行了,其他的都一样
发表于 2004-6-16 14:45:29 | 显示全部楼层
连接地址也要改的
最好用应用服务器带的连接池,一般都用jndi来连数据库的
发表于 2004-6-16 19:41:49 | 显示全部楼层
本来贴了,再贴一遍
[PHP]

/*
*ConnectionPool.java
* 创建日期 2003-10-29
* Copyright2003 hantsy<hantsy@tom.com>
*
*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.


*/
package com.demo;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionPool {
   
        private static String JNDI_NAME = "java:comp/env/jdbcdemo";
   
        private DataSource ds;
        private static ConnectionPool mySelf;
   
        private ConnectionPool(DataSource ds) {
                this.ds = ds;
        }
   
        public static ConnectionPool getInstance() {
        
                try {
            
                        if(mySelf == null) {
               
                                Context initCtx = new InitialContext();
                                DataSource ds = (DataSource)initCtx.lookup(JNDI_NAME);
               
                                mySelf = new ConnectionPool(ds);               
               
                        }
                        return mySelf;
            
                } catch(NamingException ex) {
                        ex.printStackTrace();
                        throw new RuntimeException("connect to database failed!");
                }
        
        }
   
        public Connection getConnection(boolean autoCommit) throws SQLException {
        
                Connection con = ds.getConnection();
                con.setAutoCommit(autoCommit);
                return con;
        }
   
}[/PHP]
发表于 2004-6-16 19:45:43 | 显示全部楼层
那个图标是: c o m p(连起来)
发表于 2004-6-16 20:11:32 | 显示全部楼层
楼上的可以在发贴时通过“预览回复”禁止表情符号。
发表于 2004-6-16 22:55:13 | 显示全部楼层
最好是用php代码来,像我的那样,看起来好很多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表