JAVA数据库连接

发布于 / Java / 0 条评论

先导入sql驱动包库

核心代码就几行

String URL="jdbc:mysql://localhost:3306/Test";

String user="root";

String pwd="xxx";

String sql="insert Employee(empAccount,empPassword) values(?,?)";

Connection conn=null;

PreparedStatement stmt=null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn=DriverManager.getConnection(URL, user, pwd);

stmt=conn.prepareStatement(sql);

stmt.setString(1, username);

stmt.setString(2, password);

int result =stmt.executeUpdate();

if(result>0) {

JOptionPane.showMessageDialog(null, "增加成功!", "OK",JOptionPane.INFORMATION_MESSAGE);

}

else {

JOptionPane.showMessageDialog(null, "增加失败!", "NO",JOptionPane.ERROR_MESSAGE);

}

}

catch(Exception e2) {

System.out.println(e2.getMessage());

}

finally {

try {

stmt.close();

}

catch(Exception e1) {

System.out.println(e1.getMessage());

}

}

}

转载原创文章请注明,转载自: 静沐暖阳 » JAVA数据库连接
Not Comment Found