while trying to connect to mysql database by java i get the following error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'dbname'
i wrote the same code while the db was on local host and it worked but after deleting the previous connection and creating new tables in new connection gives the error.
I have already created the db dbname
in mysql in a new connection in new server instance
please help me by telling how to get the db domain from mysql and how to resolve this issue
my code is following:
public static void main(String[] args) throws ClassNotFoundException, SQLException, FileNotFoundException
{
try
{
int i;
int numcol = 1;
int j;
int count = 0;
int c = 0;
Class.forName("com.mysql.jdbc.Driver");
Connection con=null;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname","root", "ellimist");
Statement st=(Statement) con.createStatement();
FileInputStream file = new FileInputStream(new File("C:\\Users\\krayak\\Desktop\\Final.xlsx"));
XSSFWorkbook workbook=new XSSFWorkbook(file);
XSSFSheet sheet=workbook.getSheetAt(0);
List sheetdata=new ArrayList();
List a=new ArrayList();
int numrow=sheet.getPhysicalNumberOfRows();
Iterator<Row> rowiterator=sheet.rowIterator();
while(rowiterator.hasNext())
{
Row row =rowiterator.next();
List data=new ArrayList();
for(i=0;i<numcol;i++)
{
XSSFCell cell;
if(row.getCell(i)==null)
{
cell=(XSSFCell) row.createCell(i);
data.add(cell);
}
else
{
cell=(XSSFCell) row.getCell(i);
data.add(cell);
}
}
sheetdata.add(data);
}
file.close();
for(i=numrow;i<sheetdata.size();i++)
{
List list=(List) sheetdata.get(i);
for(j=0;j<numcol;j++)
{
XSSFCell cell=(XSSFCell) list.get(j);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
a.add(cell.getNumericCellValue());
System.out.println(cell.getNumericCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_STRING)
{
a.add(cell.getRichStringCellValue());
System.out.println(cell.getRichStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
{
a.add(cell.getBooleanCellValue());
System.out.println(cell.getBooleanCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_BLANK)
{
a.add(null);
System.out.println(cell);
}
}
count++;
}
for(i=0;i<count;i++)
{
String sql="insert into testing.test (name) values ('";
sql += a.get(c++) + "')";
st.executeUpdate(sql);
}
}
catch(NullPointerException e)
{
System.out.println("null pointer exception"+e);
e.printStackTrace();
}
catch(SQLException ex)
{
System.out.println("sqlexception "+ex);
}
catch(FileNotFoundException ex)
{
System.out.println("file not found "+ ex);
}
catch(Exception ex)
{
System.out.println("exception\n"+ex);
}
}
}