JDBC之 MYSQL

类别:Java 点击:0 评论:0 推荐:

package zmsjdbc;
import java.sql.*;
public class mysql1
{

  private   String url="jdbc:mysql://localhost:3306/beyond";
            //先下载MYSQL 驱动  mm.mysql-2.0.4-bin.jar 放在D:\JBuilder9\jdk1.4\jre\lib\ext 下

 private   String user="root";
 private   String pwd="";
 public   Connection  getcon()
 {
   Connection con;
    try
    { //加载驱动程序
      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
      //创建连接
      con=DriverManager.getConnection(url,user,pwd);
      return con;
     }

     catch(ClassNotFoundException e)
     {
         System.out.println("加载驱动程序出错");
     }


     catch(Exception e)
     {
       System.out.println("出现了好大的错误");
     }

     return null;
 }
 public  ResultSet  executeSql(String sqlstr)
   {
     Connection conn;
     Statement stmt;

      try
      {
        conn=getcon();
        stmt=conn.createStatement();
        ResultSet  rs=stmt.executeQuery(sqlstr);

        return rs;
      }
      catch(SQLException e)
      {
        System.out.print("获取数据集时出现错误");
      }
      return null;
   }

  public static void main(String[] args)
  {
    mysql1 mysql11 = new mysql1();
    Connection conn;
    ResultSet  rs;
   rs= mysql11.executeSql("select * from zms1");
    try
    {
      while(rs.next())
      {
        System.out.println(rs.getString("name"));
      }
    }
    catch(Exception e)
    {
       System.out.println(e.getMessage());
    }


  }

}

本文地址:http://com.8s8s.com/it/it11222.htm