import javax.comm.*;
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ListPortClass3
{
public static void main(String[] s)
{
String str="AT+CMGF=1\r";
int k=1;
try{
input(str,0);
Thread.sleep(1000);
input("AT+CPMS=\"SM\"\r",0);
Thread.sleep(1000);
while(true){
input("AT+CMGR="+k+"\r",1);
Thread.sleep(1000);
k++;
}
}
catch(Exception e){}
}
public static void input(String mValue,int flag)
{
//String mValue = null;
//strcpy(mValue,str1);
try
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM14");
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Port in use!");
}
else {
System.out.println(portIdentifier.getName());
SerialPort serialPort = (SerialPort) portIdentifier.open("ListPortClass", 9600);
int b = serialPort.getBaudRate();
System.out.println(Integer.toString(b));
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream mOutputToPort = serialPort.getOutputStream();
InputStream mInputFromPort = serialPort.getInputStream();
//String mValue = "AT+CPMS=\"SM\" \r";
System.out.println("beginning to Write . \r\n");
//while(a<=2)
//{
mOutputToPort.write(mValue.getBytes());
System.out.println("AT Command Written to Port. \r\n");
mOutputToPort.flush();
Thread.sleep(500);
//a++;
//}
byte[] mBytesIn = new byte[1024];
int i=0;
int len=-1;
while(true)
{
//len=mInputFromPort.read(mBytesIn);
if ( (len = mInputFromPort.read(mBytesIn))> -1)
{
String s=new String(mBytesIn,0,len);
System.out.println(s);
//System.out.println(len);
//mBytesIn=null;
//len=0;
if(flag==1){
String str2[]=s.split("\"");
data(str2[3],str2[5],str2[6]); // for database
for(int j=0;j<str2.length;j++)
{
System.out.println(j+"="+str2[j]);
//System.out.println("2="+"="+str2[4]);
}
}
//if(s.charAt(len-1)=='K' && s.charAt(len-2)=='O')
break;
}
else
{
System.out.print("*");
break;
}
}
//mOutputToPort=null;
//mInputFromPort=null;
//serialPort=null;
serialPort.close();
}
}
catch (Exception ex)
{
System.out.println("Exception : " + ex.getMessage());
}
}
public static void data(String first,String second,String third)
{
String url = "jdbc:mysql://localhost:3306/";
String db = "phd";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass= "";
String data1=null;
try{
Connection con=null;
System.out.println("database");
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st=con.createStatement();
Statement st1=con.createStatement();
st1.executeUpdate("insert into sms(mbno,dateandtime,content) values('"+first +"','" +second + "','"+third+"')");
ResultSet rs=st.executeQuery("select * from sms");
while(rs.next())
{
data1 = ":" + rs.getString("id") + ": " + rs.getString("mbno")+":"+rs.getString("dateandtime")+":"+rs.getString("content")+":";
System.out.println(data1+" ");
}
}
catch (SQLException s){
System.out.println("No any table in the database");
}
catch (ClassNotFoundException e){
System.err.println(e.getMessage());
}
catch(Exception e){System.out.println(e);}
}
}
No comments:
Post a Comment