import java.sql.*;
import java.util.*;
public class app1conn {
String url="jdbc:mysql://localhost:3306/test",uname,password;
Connection con;
PreparedStatement ps;
app1conn() throws SQLException{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con= DriverManager.getConnection(url,"root","");
}
public void insert (String name,String email,String age,String dob)throws Exception{
ps=con.prepareStatement(url);
//ps=new con.PreparedStatement ("insert into info(name,email,age,dob) values(?,?,?,?)") ;
ps.setString(1,name);
ps.setString(2,email);
ps.setString(3,age);
ps.setString(4,dob);
ps.executeUpdate();
}
public Vector getData() throws SQLException{
ps=con.prepareStatement("Select * from info");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rms=rs.getMetaData();
Vector data=new Vector();
while(rs.next()){
Vector temp=new Vector();
temp.add(rs.getString(1));
temp.add(rs.getString(2));
temp.add(rs.getString(3));
temp.add(rs.getString(4));
temp.add(rs.getString(5));
data.add(temp);
}
return data;
}
public Vector getDataById(String id) throws SQLException{
ps=con.prepareStatement("Select * from info where id=?");
ps.setInt(1,Integer.parseInt(id));
ResultSet rs=ps.executeQuery();
ResultSetMetaData rms=rs.getMetaData();
Vector data=new Vector();
while(rs.next()){
Vector temp=new Vector();
temp.add(rs.getString(1));
temp.add(rs.getString(2));
temp.add(rs.getString(3));
temp.add(rs.getString(4));
temp.add(rs.getString(5));
data.add(temp);
}
return data;
}
void update_info(String name,String email,String age,String dob,String id) throws SQLException{
ps=con.prepareStatement ("update info set name=?,email=?,age=?,dob=? where id=?");
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, age);
ps.setString(4, dob);
ps.executeUpdate();
}
}
我正在尝试插入数据.. 当我点击提交按钮..
它将null显示为输出..
答案 0 :(得分:1)
String insertSQL = "insert into info(name,email,age,dob) values(?,?,?,?)";
PreparedStatement ps = dbConnection.prepareStatement(insertSQL);
ps.setString(1,name);
ps.setString(2,email);
ps.setString(3,age);
ps.setString(4,dob);
ps.executeUpdate();
或者
String insertTableSQL = "INSERT INTO DBUSER"
+ "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
+ "(?,?,?,?)";
PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "abc");
preparedStatement.setString(3, "system");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
// execute insert SQL stetement
preparedStatement .executeUpdate();
请参阅this article
答案 1 :(得分:0)
你有没有跟踪你的代码,你在哪一行得到空值???
尝试使用此代码代替您的代码。
public boolean insert (String name,String email,String age,String dob){
PreparedStatement preparedStatement;
try{
preparedStatement = con.prepareStatement("insert into info(name, email, age, dob) values(?,?,?,?)");
preparedStatement.setString(1,name);
preparedStatement.setString(2,email);
preparedStatement.setString(3,age);
preparedStatement.setString(4,dob);
preparedStatement.executeUpdate();
}
catch (Exception e) {
return false;
}
return true;
}