62,621
社区成员
发帖
与我相关
我的任务
分享
import java.sql.*;
public class PhoneKey {
public static void main(String[] args) {
// TODO Auto-generated method stub
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
String connectDB="jdbc:sqlserver://localhost:1433;DatabaseName=GOC_01";
String user="sa";
String pass="400";
try
{
Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类
}catch(ClassNotFoundException e)
{
System.out.println("数据库加载失败!");
e.printStackTrace();
System.exit(0);
}
try{
Connection con=DriverManager.getConnection(connectDB,user,pass);
System.out.println("数据库连接成功!");
String sql="select C_phone from ceshi_2";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while (rs.next())
{
String s=rs.getString("C_phone");
String s1=s.substring(0, 5);
String s2=s.substring(5, 13);
String biaoji="";
if(s1=="86130")
{
biaoji="K"+s2;
}
else if(s1=="86131")
{
biaoji="J"+s2;
}
else if(s1=="86132")
{
biaoji="I"+s2;
}
else if(s1=="86155")
{
biaoji="H"+s2;
}
else if(s1=="86156")
{
biaoji="G"+s2;
}
else if(s1=="86186")
{
biaoji="F"+s2;
}
else if(s1=="86185")
{
biaoji="E"+s2;
}
else {
biaoji="WRONG"+s2;
}
String sqlupdate="update ceshi_2 set C_phone = ?" ;
pstmt.setString(1, biaoji);
pstmt.executeUpdate(sqlupdate);
}
}
catch(SQLException e)
{
System.out.println("数据库操作失败!");
e.printStackTrace();
System.exit(0);
}
}
}
String sqlupdate="update ceshi_2 set C_phone = '"+biaoji+"' where ceshi_2='"+s+"'" ;
Statement stmt=con.createStatement();
stmt.executeUpdate(sqlupdate);
String sqlupdate="update ceshi_2 set C_phone = ?" ;
pstmt.setString(1, biaoji);
pstmt.executeUpdate(sqlupdate);
改为这个试试
String sqlupdate="update ceshi_2 set C_phone = ?" ;
pstmt=con.prepareStatement(sqlupdate);
pstmt.setString(1, biaoji);
pstmt.executeUpdate();String sqlupdate="update ceshi_2 set C_phone = ?" ;
pstmt=con.prepareStatement(sql);
pstmt.setString(1, biaoji);

