eclipse中java操作mysql数据库注意事项
2011-08-14 17:13
447 查看
1. 下载mysql JDBC驱动:例如mysql-connector-java-5.1.12(pudn上有);
2. 在eclipse里的项目中建立lib目录,把mysql-connector-java-5.1.12-bin-jar拷贝到这个目录中,然后在项目的build path里把这个jar包作为external jar加入到项目中;
3. import java.sql.*;
import com.mysql.jdbc.Driver;
4.测试
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("mysql驱动加载成功!");
connect = DriverManager.getConnection("jdbc:mysql://localhost:6033/mslog","root","root");
System.out.println("数据库连接成功!");
stmt = connect.createStatement(); /* -----创建statement对象,用于向数据库发送sql语句----- */
}
catch (Exception e) {
System.out.print("mysql驱动加载失败!");
e.printStackTrace();
return ;
}
5. insert含变量时的sql语句的构造
String ipAddress=GetIpAdress(lineContent);
String reqTime=GetReqTime(lineContent);
String keyword=GetKeyword(lineContent,"keyword=");
//String pageNum=GetPageNum(lineContent);
String sourcePage=GetSourcePage(lineContent);
String hitItem=GetHitItem(lineContent);
String hitUrl=GetHitUrl(lineContent);
String type=GetType(lineContent);
if(type==null)
type="0";
String sqlStr="insert into hit(ipAddress,reqTime,keyword,sourcePage,hitItem,hitUrl,type) values("+"'"+ipAddress+"',"+"'"+reqTime+"',"+"'"+keyword+"',"+sourcePage+","+hitItem+","+"'"+hitUrl+"',"+type+")" ;//11,'北京','北京')";
//String sqlStr="insert into hit(ipAddress,reqTime,keyword,sourcePage,hitItem,hitUrl,type) values('218.202.4.135','2011-08-11 00:00:00','女色网',3,8,'http://97.24644.com/',1)";
System.out.println(sqlStr);
insertDB(sqlStr);
/////////////
private void insertDB(String sqlStr)
{
if(stmt!=null)
{
//插入数据库记录
try{
stmt.executeUpdate(sqlStr);//.executeQuery(sqlStr);
}
catch(Exception e)
{
System.out.println("插入记录失败:"+sqlStr);
e.printStackTrace();
}
}
}
2. 在eclipse里的项目中建立lib目录,把mysql-connector-java-5.1.12-bin-jar拷贝到这个目录中,然后在项目的build path里把这个jar包作为external jar加入到项目中;
3. import java.sql.*;
import com.mysql.jdbc.Driver;
4.测试
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("mysql驱动加载成功!");
connect = DriverManager.getConnection("jdbc:mysql://localhost:6033/mslog","root","root");
System.out.println("数据库连接成功!");
stmt = connect.createStatement(); /* -----创建statement对象,用于向数据库发送sql语句----- */
}
catch (Exception e) {
System.out.print("mysql驱动加载失败!");
e.printStackTrace();
return ;
}
5. insert含变量时的sql语句的构造
String ipAddress=GetIpAdress(lineContent);
String reqTime=GetReqTime(lineContent);
String keyword=GetKeyword(lineContent,"keyword=");
//String pageNum=GetPageNum(lineContent);
String sourcePage=GetSourcePage(lineContent);
String hitItem=GetHitItem(lineContent);
String hitUrl=GetHitUrl(lineContent);
String type=GetType(lineContent);
if(type==null)
type="0";
String sqlStr="insert into hit(ipAddress,reqTime,keyword,sourcePage,hitItem,hitUrl,type) values("+"'"+ipAddress+"',"+"'"+reqTime+"',"+"'"+keyword+"',"+sourcePage+","+hitItem+","+"'"+hitUrl+"',"+type+")" ;//11,'北京','北京')";
//String sqlStr="insert into hit(ipAddress,reqTime,keyword,sourcePage,hitItem,hitUrl,type) values('218.202.4.135','2011-08-11 00:00:00','女色网',3,8,'http://97.24644.com/',1)";
System.out.println(sqlStr);
insertDB(sqlStr);
/////////////
private void insertDB(String sqlStr)
{
if(stmt!=null)
{
//插入数据库记录
try{
stmt.executeUpdate(sqlStr);//.executeQuery(sqlStr);
}
catch(Exception e)
{
System.out.println("插入记录失败:"+sqlStr);
e.printStackTrace();
}
}
}
相关文章推荐
- java操作cookie注意事项
- java中io操作的注意事项
- 安装java和eclipse的一些注意事项
- java 操作mysql 注意事项
- Java byte 位移操作 注意事项
- java 模拟 POST表单操作 HTTPCLIENT的应用 及注意事项
- MFC操作MySQL数据库注意事项
- Eclipse中查看Java源代码的方法及一些注意事项
- eclipsejava项目部署编译注意事项
- JAVA的简单操作注意事项
- java的文件操作注意事项
- Java利用HttpClient模拟POST表单操作应用及注意事项
- Eclipse编写java web注意事项
- eclipse javaee版发布web项目的设置注意事项
- java字符流操作flush()方法及其注意事项
- Eclipse下 Java对MySQL数据库操作
- java三目运算符操作注意事项
- Java项目引入eclipse注意事项
- Java操作数据库类Statement之注意事项!...
- 2015/12/29 Java语言概述 操作中注意事项