看下面代码,其中的("insert into dept values(?,?,?)");中的三个问号是什么意思,三问号前面加个null呢?
源代码如下:packagecom.wepull.jdbc.test;importjava.sql.Connection;importjava.sql.DriverMana...
源代码如下:
package com.wepull.jdbc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class myjdbc {
// 查看所有数据
public void findAll() {
try {
// 1.加载驱动 Load the JDBC Driver
Class.forName("com.mysql.jdbc.Driver"); // 在com.mysql.jdbc包中的Driver.class
// 2.创建连接 Establish the Databases Connection
String url = "jdbc:mysql://localhost:3306/mydata"; //3306:为mysql的固定端口号
Connection con = DriverManager.getConnection(url, "root", "123"); //con: 变量名
// 3.创建执行语句 Create a Statement Object
PreparedStatement ps = con.prepareStatement("select * from dept"); //ps:变量名
//4. 执行 Execute a Query
ResultSet rs = ps.executeQuery(); //rs:变量名
// 5.处理结果 Process the Results
while(rs.next()){
System.out.println( rs.getInt(1) +" " + rs.getString(2) + " " + rs.getString(3));
}
//6.关闭连接 Close the Connection
con.close();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(int deptId,String depname,String address){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydata";
Connection con = DriverManager.getConnection(url,"root","123");
PreparedStatement ps = con.prepareStatement("insert into dept values(?,?,?)"); //就是这句 ps.setInt(1,deptId);
ps.setString(2,depname);
ps.setString(3,address);
ps.execute();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
myjdbc mj = new myjdbc();
mj.findAll();
}
} 展开
package com.wepull.jdbc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class myjdbc {
// 查看所有数据
public void findAll() {
try {
// 1.加载驱动 Load the JDBC Driver
Class.forName("com.mysql.jdbc.Driver"); // 在com.mysql.jdbc包中的Driver.class
// 2.创建连接 Establish the Databases Connection
String url = "jdbc:mysql://localhost:3306/mydata"; //3306:为mysql的固定端口号
Connection con = DriverManager.getConnection(url, "root", "123"); //con: 变量名
// 3.创建执行语句 Create a Statement Object
PreparedStatement ps = con.prepareStatement("select * from dept"); //ps:变量名
//4. 执行 Execute a Query
ResultSet rs = ps.executeQuery(); //rs:变量名
// 5.处理结果 Process the Results
while(rs.next()){
System.out.println( rs.getInt(1) +" " + rs.getString(2) + " " + rs.getString(3));
}
//6.关闭连接 Close the Connection
con.close();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(int deptId,String depname,String address){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydata";
Connection con = DriverManager.getConnection(url,"root","123");
PreparedStatement ps = con.prepareStatement("insert into dept values(?,?,?)"); //就是这句 ps.setInt(1,deptId);
ps.setString(2,depname);
ps.setString(3,address);
ps.execute();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
myjdbc mj = new myjdbc();
mj.findAll();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询