ExcuteSQL.java
package com.sh.lw;import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Properties;public class ExcuteSQL {
   private String user;
   private String driver;
   private String pass;
   private String url;
   Connection conn;
   ResultSet rs;
   Statement stmt;
 public void executeSql(String sql) throws Exception {
 try{
  Class.forName(driver);
  conn = DriverManager.getConnection(url,user,pass);
  stmt = conn.createStatement();
  boolean hasResultSet = stmt.execute(sql);
   if(hasResultSet) {
   rs = stmt.getResultSet();
   ResultSetMetaData rsmd = rs.getMetaData();
   int columnCount = rsmd.getColumnCount();
      while(rs.next()) {
        for(int i=0; i<columnCount; i++) {
        System.out.println(rs.getString(i+1)+"\t");
        }
         System.out.println("\n");
      }  
     }  else {
       System.out.println("本次SQL语句影响的记录有"+stmt.getUpdateCount()+"条。");
   } 
   } finally { 
    if(rs!=null) {
     rs.close();
    }
     if(stmt!=null) {
      stmt.close();
     }
      if(conn!=null) {
       conn.close();
      }  
   } 
  } 
           public void initParam(String newFile) throws Exception {
               Properties props = new Properties();
               props.load(new FileInputStream(newFile));
               driver = props.getProperty(driver);
               url = props.getProperty(url);
               user = props.getProperty(user);
               pass = props.getProperty(pass);
           }
     public static void main(String[] args) throws Exception{
   ExcuteSQL es = new ExcuteSQL();
    es.initParam("src//com//sh//lw//mysql.ini");
    System.out.println("---------执行删除表的DDL语句---------");
    es.executeSql("delete table if exists my_test");
    System.out.println("--------执行创建表的DDL语句---------");
    es.executeSql("create table my_test(" + "" +
     "  test_id int  auto_increment primary key  ," 
      + " test_name varchar(255));");
    System.out.println("--------执行插入数据的DML语句----------");
    es.executeSql("insert table my_test(test_name) select student_name from student_table;");
    System.out.println("------执行查询数据的SQL语句--------");
    es.executeSql("select * from my_test;");
}
}mysql.ini  path: src//com//sh//lw//mysql.inidriver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/select_test
user=root
pass=admin下面是报的异常
Exception in thread "main" java.lang.NullPointerException
at java.util.Hashtable.get(Hashtable.java:334)
at java.util.Properties.getProperty(Properties.java:932)
at com.sh.lw.ExcuteSQL.initParam(ExcuteSQL.java:53)
at com.sh.lw.ExcuteSQL.main(ExcuteSQL.java:60)我的mysql驱动包,导进去了,不存在问题。

解决方案 »

  1.   

    我说private String user;
    private String driver;
    private String pass;
    private String url;值呢????
      

  2.   

    at com.sh.lw.ExcuteSQL.main(ExcuteSQL.java:60)
    60行
      

  3.   

    es.initParam("src//com//sh//lw//mysql.ini");
    路径不对
      

  4.   


    private String user = "user";
    private String driver = "driver";
    private String pass = "pass";
    private String url = "url";
      

  5.   

    插入java代码吧, 把行数什么地都显示出来, 
      这个问题不难, 估计就是上面说的问题
      

  6.   


    路径没问题,要不然就应该报FileNotFound Exception,而不是空指针
      

  7.   

    es.initParam("src//com//sh//lw//mysql.ini");
    就是路径不对。你在initParam里面try catch一下。
      

  8.   

    driver = props.getProperty(driver);
    url = props.getProperty(url);
    user = props.getProperty(user);
    pass = props.getProperty(pass);少了引号吧~~
    driver = props.getProperty("driver");你的driver都没定义,getProperty还能get出来什么?
      

  9.   

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/select_test
    user=root
    pass=admin
    上面应该是把字符串赋值吧,引号呢?????