尝试通过java连接到mysql数据库时出现以下错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Unknown 数据库'dbname'
我在db在本地主机上时编写了相同的代码并且它工作但是在删除之前的连接并在新连接中创建新表后会产生错误。
我已经在新服务器实例的新连接中在mysql中创建了db dbname
请告诉我如何从mysql获取db域以及如何解决此问题
我的代码如下:
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);
}
}
}