public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
FileInputStream fis=new FileInputStream("F:\\JAVA\\JDBC\\src\\JDBC\\person.txt");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String ss;
String s[];
boolean i=false;
Connection cc=ConnectionFactory.getConnection();
Statement stmt = cc.createStatement();
while(br.read()!=-1){
ss=br.readLine();
System.out.println(ss);
}
}
为什么打印出来每行开头都少一个字符啊;
----------------解决方案--------------------------------------------------------
你先读了,再读行,这当然会少一个字符
应该改为
while((ss=br.readLine())!=null){
System.out.println(ss);
}
----------------解决方案--------------------------------------------------------
我为什么这个API 总是弄不熟啊 这是为什么啊
有没有什么好办法去训练啊
----------------解决方案--------------------------------------------------------
多练多想,
就可以了
----------------解决方案--------------------------------------------------------
如何验证我的JDBC是否已经成功连接到我的my sql
啊。除了在java中写select 类似这样方法还有没有别的方法
----------------解决方案--------------------------------------------------------
不抛异常就应该连上了
----------------解决方案--------------------------------------------------------
那就奇怪了
package JDBC;
import java.io.IOException;
import java.sql.*;
public class MysqlData {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
String i="111";
Connection con=ConnectionFactory.getConnect();
try{
PreparedStatement stmt = con.prepareStatement("select item_id from item where item_id=?");
stmt.setString(0,"002");
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
i=rs.getString("item_id");
System.out.println(i);
}
rs.close();
stmt.close();
}catch(Exception e){
}finally{
con.close();
}
}
为什么我这个打印不出东西来啊 我的数据库里有记录啊
工厂类:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class ConnectionFactory {
static Properties pr;
static Connection con=null;
static String dir=null;
static String url1=null;
static String user=null;
static String ps=null;
public static void initParams(String paramFileLocaltion) throws IOException{
FileInputStream fi=new FileInputStream(paramFileLocaltion);
pr= new Properties();
pr.load(fi);
dir=pr.getProperty("Dir");
url1=pr.getProperty("url");
user=pr.getProperty("user");
ps=pr.getProperty("ps");
}
public static Connection getConnect() throws ClassNotFoundException, SQLException, IOException{
if(con==null||con.isClosed()){
ConnectionFactory.initParams("F:\\JAVA\\HomeWork\\src\\parm.txt");
Class.forName(dir);
con=DriverManager.getConnection(url1,user,ps);
}
return con;
}
}
----------------解决方案--------------------------------------------------------
catch(Exception e){
}
你这就算是抛出了异常你也不知道啊
你什么都没做
你应该在捕做异常的时候做点什么,把它打印了来也好啊,至少你也知道出异常了
像你这样空方法是非常不提倡 的,也是一种非常不好的编程习惯
----------------解决方案--------------------------------------------------------
Table 'mysql.item' doesn't exist
我的表在的啊 我在数据库里都可以用sql语句查询
----------------解决方案--------------------------------------------------------
是不是数据库没有连接成功啊 我的工厂类写的有没有问题啊
----------------解决方案--------------------------------------------------------