//加载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance(); 

                  //建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost/ElectronicPolice?user=root&password=qqdiy0");
 
final String sql = "Insert Into VehicleLocate (headChar,VehiclelocateName,localFlag,specialFlag,specialType) values (?,?,?,?,?)";

PreparedStatement  pstm = conn.prepareStatement(sql);
System.out.println("声明prepareStatement");
pstm.setString(1,dataIn.headChar);
pstm.setString(2,dataIn.vehicleLocateName);
pstm.setInt(3,dataIn.localFlag);
pstm.setInt(4,dataIn.specialFlag);
pstm.setString(5,dataIn.specialType);
System.out.println("将要执行SQL");
int count=pstm.executeUpdate();
System.out.println("执行完SQL");
if (count<1)
{
throw (new Exception());
}
                                       
 
pstm.close();
conn.close();
从我的程序剪下来的,不同数据库不同连接字符串。

解决方案 »

  1.   

    sql  server  2000  ot  mysql
      

  2.   

    <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
    //pubs为你的数据库的 
    String user="sa"; 
    String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password); 
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
    String sql="select * from test"; 
    ResultSet rs=stmt.executeQuery(sql); 
    while(rs.next()) {%> 
    您的第一个字段内容为:<%=rs.getString(1)%> 
    您的第二个字段内容为:<%=rs.getString(2)%> 
    <%}%> 
    <%out.print("数据库操作成功,恭喜你");%> 
    <%rs.close(); 
    stmt.close(); 
    conn.close(); 
    %> 这个是sql server的连接程序,你把它写在类里就行了,如果是测试的话,直接写在jsp页面中就可以!
      

  3.   

    顺便问个题外话,2只女菜鸟???????????????????????“菜鸟”一词还是不用的好,每个java学习者都是从菜鸟走向成功的!
    最令我疑惑的就是,“2只”,不就你一个吗?还有那个呢??
      

  4.   

    假如我要对两个表进操作那我的 javabean要怎么改??String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
    //pubs为你的数据库的 
      

  5.   

    dbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs
    你说的两个表都在pubs这个数据库用户里吧,那就不需要改