直接上代码
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class MyTest {
public static void main(String args[]) {
Connection con = null;
try {
// 注册JDBC驱动,JAVA1.5以后 JDBC自动加载驱动了 所以这句代码可以省略;
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 提供地址用户名密码并获得连接对象
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root", "");
if (!con.isClosed())
// 连接成功提示
System.out.println("Successfully connected to MySQL server using TCP/IP...");
// 有Connection对象创建PreparedStatement
PreparedStatement ps= con.prepareStatement("select * from orderitem o where o.itemid >?");
// 设置参数,参数索引位置是从1开始