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驱动包,导进去了,不存在问题。
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驱动包,导进去了,不存在问题。
解决方案 »
- 不显示删除回复显示所有回复显示星级回复显示得分回复 这段代码的简单的类图设计如图以及对象协作图
- 课程设计,都快把我搞麻了...谁能帮忙看一下...MS很简单,但就是搞不明白...
- 谁愿意指点一下我啊!!
- 问个比较初级的问题native2ascii不是内部命令
- 请问各们达人一个关于树拖动的错误:Drag and drop in progress
- servlet中文编码出现乱码的问题
- 急呀,请问JXL或者POI 生成的EXCEL怎么直接打印到打印机上。
- 请教:java中汉字是以什么方式排序的?
- 各位大哥请帮忙
- 自动编译执行java程序的程序
- 求一个JAVA简单的数据库操作的例子。顺便求几本学习JAVA的好书。
- java中滚动条如何
private String driver;
private String pass;
private String url;值呢????
60行
路径不对
private String user = "user";
private String driver = "driver";
private String pass = "pass";
private String url = "url";
汗
这个问题不难, 估计就是上面说的问题
路径没问题,要不然就应该报FileNotFound Exception,而不是空指针
就是路径不对。你在initParam里面try catch一下。
url = props.getProperty(url);
user = props.getProperty(user);
pass = props.getProperty(pass);少了引号吧~~
driver = props.getProperty("driver");你的driver都没定义,getProperty还能get出来什么?
url=jdbc:mysql://localhost:3306/select_test
user=root
pass=admin
上面应该是把字符串赋值吧,引号呢?????