发现以前学过的Demo
public void ConnectToDB()
{
try
{
String oURL = "localhost";
String oUser = "root";
String oPassword = "123";
String sdName = "com.mysql.jdbc.Driver";
String oDB="yaduhoutai";
Class.forName(sdName).newInstance();
String connStr = "jdbc:mysql://" + oURL +"/"+oDB+ "?user="+ oUser+"&password="+oPassword+"&useUnicode=true&characterEncoding=GB2312";
conn = DriverManager.getConnection(connStr);
}
catch(Exception e)
{
System.out.println(e);
System.out.println("数据库连接失败!");
}
}插入 public boolean add_filedown(filedownDao dao){
boolean flag=true;
String str="insert into filedown(title,urls,datatime) values('"+dao.getTitle()+"','"+dao.getUrls()+"','"+dao.getDatatime()+"')";
System.out.print(str);
DBUtil dbuitl=new DBUtil();
try{
dbuitl.ConnectToDB();
dbuitl.excuteinsert(str);
}catch(Exception e){
e.printStackTrace();
flag=false;
}
return flag;
}查询 public String toCharSet(String str)
{
if(str==null)
return "";
try
{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b); }
catch (Exception e)
{
e.printStackTrace();
}
return str;
}
public void sel(){
String str="select * from filedown where id=14";
DBUtil dbuitl=new DBUtil();
try{
dbuitl.ConnectToDB();
ResultSet rs=dbuitl.excuteResult(str);
while(rs.next()){
System.out.print(toCharSet(rs.getString(2)));
}
}catch(Exception e){
e.printStackTrace();
}
}
public void ConnectToDB()
{
try
{
String oURL = "localhost";
String oUser = "root";
String oPassword = "123";
String sdName = "com.mysql.jdbc.Driver";
String oDB="yaduhoutai";
Class.forName(sdName).newInstance();
String connStr = "jdbc:mysql://" + oURL +"/"+oDB+ "?user="+ oUser+"&password="+oPassword+"&useUnicode=true&characterEncoding=GB2312";
conn = DriverManager.getConnection(connStr);
}
catch(Exception e)
{
System.out.println(e);
System.out.println("数据库连接失败!");
}
}插入 public boolean add_filedown(filedownDao dao){
boolean flag=true;
String str="insert into filedown(title,urls,datatime) values('"+dao.getTitle()+"','"+dao.getUrls()+"','"+dao.getDatatime()+"')";
System.out.print(str);
DBUtil dbuitl=new DBUtil();
try{
dbuitl.ConnectToDB();
dbuitl.excuteinsert(str);
}catch(Exception e){
e.printStackTrace();
flag=false;
}
return flag;
}查询 public String toCharSet(String str)
{
if(str==null)
return "";
try
{
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b); }
catch (Exception e)
{
e.printStackTrace();
}
return str;
}
public void sel(){
String str="select * from filedown where id=14";
DBUtil dbuitl=new DBUtil();
try{
dbuitl.ConnectToDB();
ResultSet rs=dbuitl.excuteResult(str);
while(rs.next()){
System.out.print(toCharSet(rs.getString(2)));
}
}catch(Exception e){
e.printStackTrace();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货