连接数据库,SQL语句执行

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;public class insertToTable_2 {

public static final String userName = "root";
public static final String password = "123456";
public static final String url = "jdbc:mysql://192.168.122.148:3306/sms";
public static final String driver = "com.mysql.jdbc.Driver";

public static void main(String[] args) {
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

try {
Connection con = DriverManager.getConnection(url, userName,password);
Statement stmt = con.createStatement() ;
String sql = "INSERT INTO SMS.IN_BOX (ID,ORGN_ADRS,DSTNTN_ADRS,CNTNT,RCV_TM,IS_RCVD,MSG_TP,MSG_ID,ORGN_ADRS_TP,ACTN_ID,ACTN_RSN_ID,SRVC_ID,PRTCL_TP,IS_RDD,IS_SNSTV_WORD,IS_BLCK) VALUES("+i+",'15593771234','0015','EXCEPTIN','2013-04-07 13:12:00',NULL,0,'15509370987',0,0,'0','1',0,0,0,0);";

int rows = stmt.executeUpdate(sql) ; //返回受影响行数,对于不返回行数的语句返回0

System.out.println("rows:"+rows);

ResultSet rs = stat.executeQuery("SELECT * FROM tablename");//执行select查询时必须使用executeQuery方法

while(rs.next()){

  String str1=rs.getString(1);//数字参数访问器,数据库的系列号是从1开始的

  Double price=rs.getDouble("Price");//字符串参数访问器,

}

Boolean bool = stat.execute(sql);//可以执行任何的SQL语句,通常只用于用户提供的交互式查询
stmt.close();
con.close();

} catch (SQLException se) {
System.out.println("鏁版嵁搴撹繛鎺ュけ璐ワ紒");
se.printStackTrace();
}
}
}

posted @ 2013-08-29 10:51  562323273  阅读(433)  评论(0编辑  收藏  举报