求大神解决java连接数据库问题
sql=String.format("insertintostuinfo(stuno,stuname,sex,age,city)values('%s','%s','%s'...
sql = String.format("insert into stuinfo(stuno,stuname,sex,age,city) values('%s','%s','%s',%d,'%s')",stuno,stuname,sex,age,city);
报错:类型 String 中的方法 format(Locale, String, Object[])对于参数(String, String, String, String, int, String)不适用~~~~这个怎么回事?
主要功能就是在控制台里输入数据增加到数据库中
// 添加学生
private static void addStuinfo(){
String stuno,stuname,sex,city;
int age;
String sql = "";
// 连接对象、命令对象
Connection conn=null;
Statement stmt = null;
int result = -1;
System.out.println("请输入学号:");
stuno = input.next();
System.out.println("请输入姓名:");
stuname = input.next();
System.out.println("请输入性别:");
sex = input.next();
System.out.println("请输入年龄:");
age = input.nextInt();
System.out.println("请输入籍贯:");
city = input.next();
// 设置sql命令
sql = String.format("insert into stuinfo(stuno,stuname,sex,age,city) values('%s','%s','%s',%d,'%s')",stuno,stuname,sex,age,city);
try {
// 获取连接
conn = getConnection();
// 创建命令
stmt = conn.createStatement();
// 执行命令
result = stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
// 释放资源
stmt.close();
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if(result>0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
} 展开
报错:类型 String 中的方法 format(Locale, String, Object[])对于参数(String, String, String, String, int, String)不适用~~~~这个怎么回事?
主要功能就是在控制台里输入数据增加到数据库中
// 添加学生
private static void addStuinfo(){
String stuno,stuname,sex,city;
int age;
String sql = "";
// 连接对象、命令对象
Connection conn=null;
Statement stmt = null;
int result = -1;
System.out.println("请输入学号:");
stuno = input.next();
System.out.println("请输入姓名:");
stuname = input.next();
System.out.println("请输入性别:");
sex = input.next();
System.out.println("请输入年龄:");
age = input.nextInt();
System.out.println("请输入籍贯:");
city = input.next();
// 设置sql命令
sql = String.format("insert into stuinfo(stuno,stuname,sex,age,city) values('%s','%s','%s',%d,'%s')",stuno,stuname,sex,age,city);
try {
// 获取连接
conn = getConnection();
// 创建命令
stmt = conn.createStatement();
// 执行命令
result = stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
// 释放资源
stmt.close();
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if(result>0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询