ResultSetMetaData ramd = rs.getMetaData()
ramd.getColumnType()//列字短的类型
ramd.getColumnName()//列字段的名字
ramd.getColumnCount()//有多少列
创建一个以JDBC连接数据库的程序,包含7个步骤: 1、加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的静态方法forName(String className)实现。我刚写了一个只有插入的,望采纳import java.sql.*
import java.util.*
public class TestPre {
public static void main(String[] args) {
int i=0,deptno=0//i只做while循环使用,deptno是表dept2中的一个属性,类型是int
String dname=null,loc=null//dname和loc也是表dept2的属性,类型是String
Scanner s=new Scanner(System.in)
System.out.println("请输入3个参数")
while(i<3){
try{
deptno=s.nextInt()
i++
dname=s.next()
i++
loc=s.next()
i++
}catch(InputMismatchException e){
System.out.println("输入的类型不符,退出")
System.exit(-1)
}
}
Connection conn=null
PreparedStatement pstmt=null
try {
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"+ "user=root&password=root")
pstmt=conn.prepareStatement("insert into dept2 values(?,?,?)")
pstmt.setInt(1, deptno)
pstmt.setString(2, dname)
pstmt.setString(3, loc)
pstmt.executeUpdate()
System.out.println("插入完成")
} catch (ClassNotFoundException e) {
System.out.println("连接数据库不成功,程序退出")
System.exit(-1)
} catch (SQLException e) {
System.out.println("连接数据库不成功,程序退出")
System.exit(-1)
}
finally{
try{
if(pstmt!=null){
pstmt.close()
pstmt=null
}
if(conn!=null){
conn.close()
conn=null
}
}catch(SQLException e){
e.printStackTrace()
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)